문제


필요 지식

  1. 문자열

해결 방법

p 개수, y 개수 따로 세지 않고, 둘의 수가 같은 지 보는 것이므로 하나의 변수만 사용했다. p를 발견하면 변수++, y를 발견하면 변수--해서 변수가 0이면 둘의 수가 같으므로 true를 return하는 식으로 하였다.


코드

#include <string>
#include <iostream>
using namespace std;
bool solution(string s)
{
bool answer = false;
int pair = 0;
for(auto& c : s){
if(c=='p'||c=='P') pair++;
else if(c=='y'||c=='Y') pair--;
}
if(!pair) answer = true;
return answer;
}