문제 출처
문자열 내 p와 y의 개수
내가 푼 풀이
class Solution {
boolean solution(String s) {
boolean answer = true;
char[] a = s.toCharArray();
int p_count = 0;
int y_count = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == 'p' || a[i] == 'P') {
p_count++;
}
if (a[i] == 'y' || a[i] == 'Y') {
y_count++;
}
}
if (p_count != y_count) {
answer = false;
}
return answer;
}
}
다른 사람들의 풀이
class Solution {
boolean solution(String s) {
s = s.toUpperCase();
return s.chars().filter( e -> 'P'== e).count() == s.chars().filter( e -> 'Y'== e).count();
}
}