Algorithm/프로그래머스

[Lv. 1] 문자열 내 p와 y의 개수

녱녱 2023. 4. 24.

class Solution {
    boolean solution(String s) {
        boolean answer = true;
        char ch;
        int cnt = 0;
        
        for(int i = 0; i < s.length(); i++){
            ch = s.charAt(i);
            if(ch == 'p' || ch == 'P')
                cnt ++;
            else if(ch == 'y' || ch == 'Y')
                cnt --;
            if(cnt != 0){
                answer = false;
            }else{
                answer = true;
            }
        }
        return answer;
    }
}

 

댓글