題目:
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"].
hashmap + string操作。
時間:O(N)
class Solution {public: vector<string> findRepeatedDnaSequences(string s) { vector<string> res; if (s.size() < 10) return res; unordered_map<string, int> strs; for (int i = 0; i <= s.size() - 10; i++) { strs[s.substr(i, 10)]++; } for (unordered_map<string, int>:: iterator it = strs.begin(); it != strs.end(); it++) { if (it->second > 1) res.push_back(it->first); } return res; }};
該方法的缺點是由於把string用作key,空間佔用過大
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
vector<string> res;
if (s.size() < 10) return res;
unordered_map<char,int> change;
change['A'] = 0;
change['C'] = 1;
change['G'] = 2;
change['T'] = 3;
unordered_map<int, int> strs;
int num = 0;
for (int i = 0; i < s.size(); i++) {
num = num << 2 | change[s[i]];
if (i < 9) continue;
num &= 0xfffff;
if (strs.count(num) && strs[num] == 1) {
res.push_back(s.substr(i - 9, 10));
}
strs[num]++;
}
return res;
}
};