Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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 31
Tags
more
Archives
Today
Total
관리 메뉴

코딩응급실

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

Java

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

Daeryuk Kim 2023. 12. 4. 17:35

public class Solution {
    public static void main(String[] args) {
        String s = "aaabbaccccabba"; // 단어

        Solution sol = new Solution();

        int result = sol.solution(s);

        System.out.println(result);
    }

    public int solution(String s) {
        int answer = 0;

        for (int x = 0; x < s.length(); x++) {
            char xAlpha = s.charAt(x);
            int xCnt = 1;
            int differCnt = 0;

            while (x + 1 < s.length() && (xCnt != differCnt)) {
                if (s.charAt(x + 1) == xAlpha) {
                    xCnt++;
                } else {
                    differCnt++;
                }
                x++;
            }
            answer++;
        }
        return answer;
    }
}