Skip to content

Commit c48c663

Browse files
committed
Refactored Repeated DNA Sequences.java
1 parent 50ca650 commit c48c663

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

Medium/Repeated DNA Sequences.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
class Solution {
2-
public List<String> findRepeatedDnaSequences(String s) {
3-
Map<String, Integer> map = new HashMap<>();
4-
5-
for (int i=0; i<=s.length() - 10; i++) {
6-
map.put(s.substring(i, i+10), map.getOrDefault(s.substring(i, i+10), 0) + 1);
7-
}
8-
9-
List<String> ans = new ArrayList<>();
10-
11-
for (Map.Entry<String, Integer> entry : map.entrySet()) {
12-
if (entry.getValue() > 1) {
13-
ans.add(entry.getKey());
14-
}
15-
}
16-
17-
return ans;
2+
public List<String> findRepeatedDnaSequences(String s) {
3+
Set<String> seenSequences = new HashSet<>();
4+
Set<String> duplicateSequences = new HashSet<>();
5+
for (int idx = 0; idx <= s.length() - 10; idx++) {
6+
String currentSequence = s.substring(idx, idx + 10);
7+
if (!seenSequences.add(currentSequence)) {
8+
duplicateSequences.add(currentSequence);
9+
}
1810
}
11+
return new ArrayList<>(duplicateSequences);
12+
}
1913
}

0 commit comments

Comments
 (0)