-
Notifications
You must be signed in to change notification settings - Fork 523
/
Copy path17. Repeated DNA Sequences.cpp
50 lines (38 loc) · 1.07 KB
/
17. Repeated DNA Sequences.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
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 1:
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
Output: ["AAAAACCCCC","CCCCCAAAAA"]
Example 2:
Input: s = "AAAAAAAAAAAAA"
Output: ["AAAAAAAAAA"]
Constraints:
0 <= s.length <= 105
s[i] is 'A', 'C', 'G', or 'T'.
*/
class Solution
{
public:
vector<string> findRepeatedDnaSequences(string s)
{
if (s.size() < 10)
return {};
unordered_map<string, int> m;
vector<string> ans;
for (int i = 0; i <= s.size() - 10; ++i)
{
string subStr = s.substr(i, 10);
m[subStr]++;
}
for (auto it = m.begin(); it != m.end(); ++it)
{
cout << it->first << " " << it->second << endl;
if (it->second > 1)
ans.push_back(it->first);
}
return ans;
}
};