Find the substring of length 3 which is present in the reverse order from the string

Find the substring of length 3 which is present in the reverse order from the string.
Ex: if the string is abcdcba (cba is the reverse of abc) so we should return cba.
And was asked to improve upon the complexity.

A:

1. 遍历字符串,取长度为3的子字符串,然后生成hash表1;

2. 反转字符串先;

3. 再遍历字符串,取长度为3的子字符串,得到hash值,然后查表1;

 

时间复杂度:O(n)

 

 

posted @ 2011-11-24 13:48  百分百好牛  阅读(174)  评论(0编辑  收藏  举报