-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
38 lines (31 loc) · 1001 Bytes
/
Solution.java
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
//
// Sliding Window, Hash Table, String(Repeated DNA Sequences)
// https://leetcode.com/problems/repeated-dna-sequences/
// Created by hyungwook on 2022/05/20.
//
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Solution {
public List<String> findRepeatedDnaSequences(String s) {
if (s.length() < 10) {
return List.of();
}
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 9; i++) {
builder.append(s.charAt(i));
}
HashMap<String, Integer> map = new HashMap<>();
List<String> answer = new ArrayList<>();
for (int i = 9; i < s.length(); i++) {
builder.append(s.charAt(i));
String key = builder.toString();
map.put(key, map.getOrDefault(key, 0) + 1);
if (map.get(key) == 2) {
answer.add(key);
}
builder.deleteCharAt(0);
}
return answer;
}
}