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.
Example:
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT" Output: ["AAAAACCCCC", "CCCCCAAAAA"]
思路:
具体思路请参考:here,这个博主真的写的不错,值得学习。
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
set<string> res, duplicate;
for (int i = 0; i + 9 < int(s.size()); i++) {
string tmp(s.begin() + i, s.begin() + i + 10);
if (duplicate.count(tmp)) res.insert(tmp);
else duplicate.insert(tmp);
}
return vector<string>(res.begin(), res.end());
}
};