# 187. Repeated DNA Sequences
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"]
# Solution
Approach 1: Hash table.
# Code (Python)
Approach 1:
# Code (C++)
Approach 1:
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
vector<string> res;
unordered_map<string,int> visited;
int lastHead = (int)s.size() - 10;
for (int i = 0; i <= lastHead; ++i)
{
string curr = s.substr(i, 10);
if (visited[curr] == 1)
res.push_back(curr);
visited[curr]++;
}
return res;
}
};