PLOD

[프로그래머스] 문자열 내 p와 y의 개수 본문

대외 활동 및 IT 지식/알고리즘 문제 풀이 정리

[프로그래머스] 문자열 내 p와 y의 개수

훌룽이 2026. 2. 14. 14:36

https://school.programmers.co.kr/learn/courses/30/lessons/12916?language=java

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

✅ 코드

import java.util.*;
import java.io.*;

class Solution {
    boolean solution(String s) {
        boolean answer = true;
        int pCount = 0;
        int yCount = 0;
                
        for(char x : s.toCharArray()) {
            if(x == 'p' || x == 'P'){
                pCount++;
            }
            else if(x == 'y' || x == 'Y'){
                yCount++;   
            }
        }
        
        if(pCount != yCount){
            answer = false;
        }
        return answer;
    }
}

💡 배운 점 & 느낀 점

1. s.toCharArray() → 문자열을 char 형태의 배열로 바꿔주는 메서드 

Comments