Repeated DNA Sequences
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
Output: ["AAAAACCCCC", "CCCCCAAAAA"]class Solution {
public List<String> findRepeatedDnaSequences(String s) {
List<String> res = new ArrayList<>();
Set<Integer> set = new HashSet<>();
if(s == null || s.length() < 10){
return res;
}
for(int i = 0; i < s.length()-9; i++){
String subStr = s.substring(i,i+10);
int key = hashcode(subStr);
if(set.contains(key) && !res.contains(subStr)){
res.add(subStr);
}else{
set.add(key);
}
}
return res;
}
public int hashcode(String s){
int hash = 0;
for(int i =0 ; i < s.length();i++){
hash = hash << 2 | mapValue(s.charAt(i));
}
return hash;
}
public int mapValue(char c){
switch (c){
case 'A':
return 0;
case 'C':
return 1;
case 'G':
return 2;
case 'T':
return 3;
default:
return 0;
}
}
}Last updated