알고리즘

프로그래머스 문자열 내 P와Y의 개수

박카스마시며코딩 2022. 7. 12. 19:17

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

저는 해당 문제를 구현을 통해 해결하였습니다.

각각의 문자를 보면서 p인지 y인지 판별하고 이를 개수를 세었습니다.

 

class Solution {
    boolean solution(String s) {
        boolean answer = isSame(s);
        
        return answer;
    }
    private static boolean isSame(String s){
        int pCnt = 0;
        int yCnt = 0;
        for(int i = 0 ; i < s.length() ; i++){
            char ch = s.charAt(i);
            if(ch == 'P' || ch == 'p'){
                pCnt++;
            }
            if(ch == 'Y' || ch == 'y'){
                yCnt++;
            }
        }
        if(pCnt == yCnt){
            return true;
        }
        return false;
    }
}