문제
필요 지식
- 문자열
해결 방법
p 개수, y 개수 따로 세지 않고, 둘의 수가 같은 지 보는 것이므로 하나의 변수만 사용했다. p를 발견하면 변수++, y를 발견하면 변수--해서 변수가 0이면 둘의 수가 같으므로 true를 return하는 식으로 하였다.
코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
0 댓글