题目
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.
Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.
For example,
Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT",
Return:
[“AAAAACCCCC”, “CCCCCAAAAA”].
思路
本题最直接的思路就是,建立一个hashtable,存储string及对应个数的map,再loop一次,找出出现次数大于1次的string并将其存储到数组里。
代码
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
vector<string> res;
if(s.size()<=10)
return res;
unordered_map<string,int> mp;
for(int i=0;i<=s.size()-10;i++)
{
mp[s.substr(i,10)]++;
}
unordered_map<string,int>::iterator it = mp.begin();
while(it!=mp.end())
{
if(it->second>1)
res.push_back(it->first);
it++;
}
return res;
}
};