[알고리즘] (프로그래머스) 문자열 내p와 y의 개수

이용찬·2022년 1월 12일
0

알고리즘

목록 보기
28/31
post-thumbnail

문제
(프로그래머스) 문자열 내 p와 y의 개수

Java 풀이

class Solution {
    boolean solution(String s) {
        boolean answer = true;
        int nump = 0;
        int numy = 0;
        char temp;
        
        for(int i = 0; i < s.length(); i++) {
            temp = s.charAt(i);
            if(temp == 'p' || temp == 'P') {
                nump++;
            }
            if(temp == 'y' || temp == 'Y') {
                numy++;
            }            
        }
        
        if(nump == numy) {
            answer = true;
        } else {
            answer = false;
        }
        
        return answer;
    }
}
profile
안녕하세요. 클래식을 즐기는 개발자, 이용찬입니다.

0개의 댓글