seunghyun Note

[프로그래머스] - 크기가 작은 부분 문자열 with JAVA 본문

코딩테스트/백준

[프로그래머스] - 크기가 작은 부분 문자열 with JAVA

승숭슝현 2024. 1. 6. 22:36

링크 : https://school.programmers.co.kr/learn/courses/30/lessons/147355

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제 풀이

java는 다른언어와 다르게 String이 비교적 자유롭지 않다.
또한 크기를 생각해서 int -> long으로 변환을 해줘야한다. runtime Error
삼항연산자 사용

class Solution {
    public int solution(String t, String p) {

        int start =0;
        int end =p.length();
        int cnt=0;
        String  tmp;
        for(int i=start;i<t.length()-end+1;i++){
            tmp = t.substring(i,i+end);
            cnt += Long.parseLong(tmp) <= Long.parseLong(p) ? 1 :0;
   
        }

        
        return cnt;
    }
}

 

728x90