늘지 않는 실력에 맘이 쪼끔 힘들어서 쉬운거 들고 왔다!
JAVA 로 코테를 보다보니 IDE를 쓰지 못해 아주 극한의 상황이라고 느껴진 적도 있었다. 그날 이후로는 IDE를 쓰지 않고 테스트페이지에서 바로 풀어보려는게 조금은 익숙해진 것 같다.
The DNA sequence is composed of a series of nucleotides abbreviated as 'A'
, 'C'
, 'G'
, and 'T'
.
"ACGAATTCCG"
is a DNA sequence.When studying DNA, it is useful to identify repeated sequences within the DNA.
Given a string s
that represents a DNA sequence, return all the 10
-letter-long sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in any order.
import java.util.*;
class Solution {
/**
The DNA sequence is composed of a series of nucleotides
'A', 'C','G','T'
1. it has to be DNA sequence which is "more than once" ! occured in the DNA molecule
2. LENGTH is 10!!
*/
public List<String> findRepeatedDnaSequences(String s) {
// IF molecule is shorter than 10 -->
List<String> ans = new ArrayList<>();
if(s.length()<10) return ans;
Map<String,Integer> map = new HashMap<>();
String cur ="";
int windStart=0;
//have to use HashMap : to store (specific sequence, the number of occur)
for(int windEnd=0;windEnd<s.length();windEnd++){
cur+= Character.toString(s.charAt(windEnd));
// CHECK ONLY in the case of that the length of window is 10 - FIXED SIZE WINDOW
// WHEN SHRINK ???? There's no need to shrink in this problem
// 길이가 고정된 윈도우이기 때문에, 한 번 10의 길이가 되면 오른쪽으로 sliding만 해주면된다.
if(windEnd-windStart+1 ==10){
// Once reach this point, just slide window.
int numb= map.getOrDefault(cur,0);
if(numb==1){
ans.add(cur);
map.put(cur,numb+1);
windStart++;
cur = cur.substring(1);
}
}
return ans;
}
import java.util.*;
class Solution {
public List<String> findRepeatedDnaSequences(String s) {
List<String> ans = new ArrayList<>();
if(s.length()<10) return ans;
Map<String,Integer> map = new HashMap<>();
String cur ="";
for(int windEnd=10;windEnd<=s.length();windEnd++){
cur = s.substring(windEnd-10,windEnd);
int numb= map.getOrDefault(cur,0);
if(numb==1){
ans.add(cur);
}
map.put(cur,numb+1);
}
return ans;
}
}
Constraints:
1 <= s.length <= 105
s[i]
is either 'A'
, 'C'
, 'G'
, or 'T'
.if(s.length()==100000 ||s.length()<10) return ans;
이거나
if(s.length()>=100000 ||s.length()<10) return ans;
이거나 둘다 2ms가 나온다.
뭘까....