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;
}
}
}