개발이 하고싶니?
[프로그래머스]문자열안에 문자열(자바) 본문
class Solution {
public int solution(String str1, String str2) {
int size = str2.length();
int answer=0;
for(int i=0; i<=str1.length()-size; i++){
int n = 0;
int cnt =0;
while(n < size)
if(str1.charAt(i+n) == str2.charAt(n)){
cnt++;
n++;
if(cnt == size){
answer=1;
break;
}
}else{
break;
}
}
return answer == 1? 1 : 2;
}
}
i의 범위를 처음에는
i<str1.length()-size;
이렇게 설정했는데... 코드를 바꾸고 바꿔도 계속 정확도가 100%가 되지않아서 한참을 헤맸다......
결국 범위에 '='를 집어넣고 나서야 해결...
i<=str1.length()-size;
이거였다니...
'코딩테스트 풀어보기' 카테고리의 다른 글
[백준 5613번]계산기 프로그램(자바) (0) | 2024.01.31 |
---|---|
[프로그래머스]배열 회전시키기(자바) (0) | 2024.01.30 |
[프로그래머스]한 번만 등장한 문자(자바) (0) | 2024.01.30 |