Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
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
Archives
Today
Total
관리 메뉴

개발이 하고싶니?

[프로그래머스]문자열안에 문자열(자바) 본문

코딩테스트 풀어보기

[프로그래머스]문자열안에 문자열(자바)

차해:) 2024. 1. 30. 23:09

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;

이거였다니...