[Programmers] 문자열 내 p와 y의 개수

Lee·2020년 11월 4일
post-thumbnail

문제 출처

문자열 내 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();
    }
}

0개의 댓글