알고리즘

(JAVA) 문자열 내 p와 y의 개수

Daniel환 2018. 10. 25. 10:50
class Solution {
    boolean solution(String s) {
        char []c = s.toUpperCase().toCharArray();
        int cntP=0;
        int cntY=0;
        for(int i=0; i<c.length; i++){
            if('P'==c[i]){
                cntP++;
            }else if('Y'==c[i]){
                cntY++;
            }            
        }
        boolean answer = true;
        if(cntP==cntY||(cntP==0&&cntY==0)){
            answer=true;
        }else answer=false;

        return answer;
    }
}
------------------------------
class Solution {
    boolean solution(String s) {
        s = s.toUpperCase();

        return s.chars().filter( e -> 'P'== e).count() == s.chars().filter( e -> 'Y'== e).count();
    }
}
출처: www.programmers.co.kr