Argorithm

[Programmers]Lv.1 크기가 작은 부분 문자열

빙응이 2023. 12. 2. 15:24


📝풀이

평범한 문자열 문제

1. length()

2. chatAt()

3. 타입 변환

3개를 가지고 잘 구현하면 된다. 

 

단 첫 시도에서 런타임에러가 나왔다.

이유는 p가 18까지라서 int 말고 long을 사용해야 하기 때문이다. 

 

class Solution {
    public int solution(String t, String p) {
        int leng = p.length();
        int answer = 0;
        
        int count = t.length() - leng+ 1; //반복 횟수 
        
        for (int i = 0; i < count ; i++) {
            String temp = "";
            for (int j = 0; j < leng; j++) {
                temp += t.charAt(i + j);
            }
            if(Long.parseLong(temp) <= Long.parseLong(p)) {
                answer++;
            }
        }
        return answer;
    }
}