pooney
article thumbnail

 

문제

 

 

 

 

내가 해결한 답

1. Stream을 이용한 방법 

boolean solution(String s) {
        String upString = s.toUpperCase();
        long pCnt = upString.chars().filter(i-> i == 'P').count();
        long yCnt = upString.chars().filter(i-> i == 'Y').count();
        return pCnt == yCnt;
    }

 

2. for문과 char를 이용한 방법 

boolean solution(String s) {
        int pCnt = 0;
        int yCnt = 0;
        for(int i=0; i<s.length(); i++){
            char c = s.charAt(i);
            if("Y".equalsIgnoreCase(String.valueOf(c))){
                yCnt++;
            }
            else if("P".equalsIgnoreCase(String.valueOf(c))){
                pCnt++;
            }
        }
        return pCnt == yCnt;
    }
profile

pooney

@pooney

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!