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:

1
2
3
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"

Output: ["AAAAACCCCC", "CCCCCAAAAA"]
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
class  {
public List<String> findRepeatedDnaSequences(String s) {

List<String> list = new ArrayList<>();
if (s == null || s.length() < 10) return list;
Map<String, Integer> map = new HashMap<>();
int startIndex = 0;
while (startIndex + 10 <= s.length()) {
String str = s.substring(startIndex, startIndex + 10);
if (map.containsKey(str)) {
map.put(str, map.get(str) + 1);
} else {
map.put(str, 1);
}
startIndex++;
}
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
for (Map.Entry<String, Integer> set: entrySet) {
String str = set.getKey();
Integer v = set.getValue();
if (v >= 2) {
list.add(str);
}
}
return list;
}
}