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"].
这题很直白,直接计数感觉太简单,会超时,于是想了种复杂的方法,提交,失望,超时。索性就试试最直接的方法,就是下面这个
class Solution { public: vector<string> findRepeatedDnaSequences(string s) { vector<string>re; if (s.size()<11) return re; map<string, int>count; for (int i = 0; i <= s.length() - 10; i++) { string str = string(s.begin() + i, s.begin() + i + 10); count[str]++; } for (map<string, int>::iterator it = count.begin(); it != count.end(); it++) if (it->second > 1) re.push_back(it->first); return re; } };
靠,竟然通过了。。。彻底无语了