본문 바로가기

프로그래머스/lv1

문자열 내 p와 y의 개수

/* 문자열 내 p와 y의 개수
 * 대문자와 소문자가 섞여있는 문자열 s가 주어집니다.
 * s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True,
 * 다르면 False를 return 하는 solution를 완성하세요.
 * 'p', 'y' 모두 하나도 없는 경우는 항상 True를 리턴합니다.
 * 단, 개수를 비교할 때 대문자와 소문자는 구별하지 않습니다.
 *
 * s            answer
 * "pPoooyY"    true
 * "Pyy"        false
 */
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class programmer_1_13 {
    static String a1 = "pPoooyY";
    static String a2 = "Pyy";
    boolean solution(String s) {
        boolean answer = true;
        String[] ar = s.split("");
        int p = 0;
        int y = 0;
        for(String i : ar){
            if(i.equals("p"|| i.equals("P"))  p++;
            if(i.equals("y"|| i.equals("Y"))  y++;
        }
        return p == y ? true : false;
    }
    public static void main(String args[]){
        programmer_1_13 t = new programmer_1_13();
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(a1));
        System.out.println("---------------------------------------");
        System.out.println("result2 = " + t.solution(a2));
        System.out.println("---------------------------------------");
    }
}
cs

'프로그래머스 > lv1' 카테고리의 다른 글

x만큼 간격이 있는 n개의 숫자  (0) 2022.12.13
문자열을 정수로 바꾸기  (0) 2022.12.13
정수 제곱근 판별  (0) 2022.12.13
자연수 뒤집어 배열로 만들기  (0) 2022.12.13
자릿수 더하기  (0) 2022.12.08