Hamutaro - Hamtaro 4

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/JAVA] ๋ฌธ์ž์—ด ๋‚ด p์™€ y์˜ ๊ฐœ์ˆ˜

carsumin 2021. 10. 13. 22:57

 

charAt ์„ ์‚ฌ์šฉํ•ด์„œ p or P๋ฉด count ์ฆ๊ฐ€, y or Y๋ฉด count ๊ฐ์†Œ์‹œ์ผœ์„œ

count๊ฐ€ 0์ด๋ฉด true, 0์ด ์•„๋‹ˆ๋ฉด false๋ฅผ ๋ฆฌํ„ดํ–ˆ๋‹ค.

 

 

class Solution {
    boolean solution(String s) {
        
        boolean answer = true;
        int count=0;
        
        for(int i=0; i<s.length(); i++){
            if(s.charAt(i)=='p' || s.charAt(i)=='P'){
                count++;
            }else if(s.charAt(i)=='y' || s.charAt(i)=='Y'){
                count--;
            }
        }
        
        if(count!=0){
            return false;
        }else{
            return answer;
        }
    }
}