본문 바로가기
Computer Science/Coding Test

c++(cpp) 프로그래머스 - 코딩 테스트 : 문자열 내 p와 y의 개수

by hzyiunn 2022. 8. 11.
728x90
반응형

다른 사람들의 코드도 나랑 비슷한 것 같아서 업로드는 하지 않았다. 걍 y,Y와 p,P의 개수를 세고 비교하면 됨.

#include <string>
#include <iostream>
using namespace std;

bool solution(string s)
{
    bool answer = true;
    int count_p = 0;
    int count_y = 0;
    for(int i = 0 ; i < s.size(); i++)
    {
        if(s[i] =='p' || s[i] == 'P')
        {
            count_p ++;
        }
        else if(s[i] == 'y' || s[i] == 'Y')
        {
            count_y ++;
        }
    }
    // [실행] 버튼을 누르면 출력 값을 볼 수 있습니다.
    if(count_p != count_y)
    {
        answer = false;
    }

    return answer;
}
728x90
반응형

댓글