본문 바로가기
Algorithm (PS)/프로그래머스

[프로그래머스 Level 1] 문자열 내 p와 y의 개수 [Java]

by 태크민 2023. 8. 15.

문제

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

 

 

코드

class Solution {
    boolean solution(String s) {
        boolean answer = true;

        s = s.toUpperCase();
        int pCnt = 0;
        int yCnt = 0;
        for(int i=0 ;i<s.length(); i++){
            if(s.charAt(i)=='P'){
                pCnt++;
            }else if(s.charAt(i)=='Y'){
                yCnt++;
            }
        }
        
        
        return pCnt==yCnt;
    }
}