- Repeated DNA
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
Example 1:
Input:
“AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT”
Output:
[“AAAAACCCCC”,“CCCCCAAAAA”]
Example 2:
Input:
“GAGAGAGAGAGA”
Output:
[“GAGAGAGAGA”]
解法1:
class Solution {
public:
/**
* @param s: a string represent DNA sequences
* @return: all the 10-letter-long sequences
*/
vector<string> findRepeatedDna(string &s) {
int n = s.size();
if (n < 10) return vector<string>();
unordered_map<string, int> strMap;
vector<string> result;
for (int i = 10; i <= n; ++i) {
string subString = s.substr(i - 10, 10);
strMap[subString]++;
}
for (auto elem : strMap) {
if (elem.second > 1) result.push_back(elem.first);
}
return result;
}
};
1763

被折叠的 条评论
为什么被折叠?



