leetcode_187
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.
Example:
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
Output: ["AAAAACCCCC", "CCCCCAAAAA"]Solutions
straight forward with hash map.
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
if (s.size() < 10) return vector<string>();
unordered_map<string, int> m;
vector<string> res;
for (int i = 0; i < s.size() - 9; i++) {
string substr = s.substr(i, 10);
m[substr]++;
if (m[substr] == 2) res.push_back(substr);
}
return res;
}
};Last updated