본문 바로가기
JAVA 코딩 알고리즘 연습/프로그래머스

프로그래머스 - 문자열 내 p와 y의 개수 / JAVA (자바) 코딩 알고리즘 연습

by easpop 2023. 2. 9.
728x90
반응형

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

		int cntp = 0;
		int cnty = 0;
		
		String a = s.toLowerCase();
		
		for (int i = 0; i < s.length(); i++) {
			if(a.charAt(i) == 'p') {
				cntp++;
			}else if(a.charAt(i) == 'y') {
				cnty++;
			}
		}
		
		if(cntp == cnty) {
			answer = true;
		}else if(cntp != cnty) {
			answer = false;
		}else if(cntp == 0 && cnty == 0) {
			answer = true;
		}

        return answer;
    }
}
728x90
반응형

댓글