본문 바로가기
코딩테스트 문제집/Programmers(Lv1)

[Programmers] Lv1: 문자열 내 p와 y의 개수(12916)

by cogito30 2025. 2. 10.
반응형

문제

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

 

풀이

더보기
더보기
class Solution {
    boolean solution(String s) {
        boolean answer = true;
        int pCount = 0;
        int yCount = 0;
        for (int i = 0; i < s.length(); ++i) {
            if (s.charAt(i) == 'p' || s.charAt(i) == 'P') {
                pCount += 1;
            } else if (s.charAt(i) == 'y' || s.charAt(i) == 'Y') {
                yCount += 1;
            }
        }

        if (pCount == yCount) {
            answer = true;
        } else {
            answer = false;
        }
        
        return answer;
    }
}

 

반응형