프로그래머스 - 문자열 나누기

youngkyu MIn·2023년 12월 11일
0

문제링크 - 프로그래머스 - 문자열 나누기

class Solution {
    public int solution(String s) {

        int answer = 0;

        while(true) {
            answer++;
            s = solution2(s);

            if(s.isEmpty()) break;
        }

        return answer;
    }

    public static String solution2(String s) {

        if(s.length() == 1) return "";

        char start = s.charAt(0);
        int startLength = 1;
        int anotherLength = 0;

        for (int i = 1; i < s.length(); i++) {
            if(s.charAt(i) == start) startLength++;
            else anotherLength++;

            if(startLength == anotherLength) return s.substring(i+1);
        }

        return "";
    }
}

메서드를 하나 더 만든게 별로 효율적이진 않은 것 같다.

(300 문제 돌파!! 얏호)

profile
한 줄 소개

0개의 댓글

관련 채용 정보