题目链接: https://leetcode.com/problems/repeated-dna-sequences/
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"].
思路: 一个简单的hash表就可以搞定, 居然这么简单就过了, 也有点太水了吧.
代码如下:
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
if(s.size() < 10) return vector<string>();
unordered_map<string, int> hash;
vector<string> result;
for(int i = 0; i < s.size()-9; i++)
{
string str = s.substr(i, 10);
if(hash.count(str) && hash[str] < 2)
result.push_back(str);
hash[str]++;
}
return result;
}
};