-
Notifications
You must be signed in to change notification settings - Fork 93
/
RepeatedDnaSequences_Solution.java
58 lines (39 loc) · 1.54 KB
/
RepeatedDnaSequences_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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package Coding;
import java.util.*;
import java.lang.*;
import java.io.*;
public class RepeatedDnaSequences_Solution {
private static final Map<Character, Integer> encodings = new HashMap<>();
static { encodings.put('A',0); encodings.put('C',1); encodings.put('G',2); encodings.put('T',3); }
private final int TWO_POW_9 = (int) Math.pow(2, 9);
public List<String> findRepeatedDnaSequences(String s) {
System.out.print(encodings.size());
Set<String> res = new HashSet<>();
List<String> solution = new ArrayList<String>();
Set<Integer> hashes = new HashSet<>();
HashMap duplicates = new HashMap<Integer, String>();
for (int i = 0, rhash = 0; i < s.length(); i++)
{
if (i > 9) rhash -= TWO_POW_9 * encodings.get(s.charAt(i-10));
rhash = 2 * rhash + encodings.get(s.charAt(i));
if (i > 8 )
{
if (duplicates.get(rhash) != null)
{
res.add(s.substring(i-9,i+1));
}
else
{
duplicates.put(rhash, "");
}
}
}
return new ArrayList<>(res);
}
public static void main(String[] args)
{
RepeatedDnaSequences_Solution sln = new RepeatedDnaSequences_Solution();
List<String> list = sln.findRepeatedDnaSequences("AAAAAAAAAAAAAAAAAAAAA");
System.out.println(list.get(0));
}
}