-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatedDna.java
More file actions
30 lines (26 loc) · 1011 Bytes
/
RepeatedDna.java
File metadata and controls
30 lines (26 loc) · 1011 Bytes
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
class RepeatedDna {
public List<String> findRepeatedDnaSequences(String s) {
Set<String> set = new HashSet(), repeated = new HashSet();
for (int i = 0; i < s.length() - 9; i++) {
String dna = s.substring(i, i + 10);
if (set.contains(dna))
repeated.add(dna);
set.add(dna);
}
return new ArrayList(repeated);
}
}
class Solution2 {
public List<String> findRepeatedDnaSequences(String s) {
Map<String, Integer> freq = new HashMap();
for (int i = 0; i < s.length() - 9; i++) {
String dna = s.substring(i, i + 10);
freq.put(dna, freq.getOrDefault(dna, 0) + 1);
}
List<String> list = new ArrayList();
for (Map.Entry<String, Integer> e : freq.entrySet())
if (e.getValue() > 1)
list.add(e.getKey());
return list;
}
}