题意:找出在字符串中出现1次以上的长度为10的字串。
思路:暴力查找,用hash表实现O(1)查找。
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
map<string, int> m;
vector<string> re;
if(s.length() < 10) return re;
string temps;
for(int i = 0; i <= s.length() - 10; ++ i) {
temps = s.substr(i, 10);
m[temps] ++;
if(m[temps] == 2) re.push_back(temps);
}
return re;
}
};