Algorithm ๐ง๐ป๐ป/ํ๋ก๊ทธ๋๋จธ์ค(Programmers)
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ๋ฌธ์์ด ๋ด p์ y์ ๊ฐ์( Level 1)
dkswnkk
2021. 10. 21. 01:01
๋ฌธ์
์ฝ๋ฉํ ์คํธ ์ฐ์ต - ๋ฌธ์์ด ๋ด p์ y์ ๊ฐ์
๋๋ฌธ์์ ์๋ฌธ์๊ฐ ์์ฌ์๋ ๋ฌธ์์ด s๊ฐ ์ฃผ์ด์ง๋๋ค. s์ 'p'์ ๊ฐ์์ 'y'์ ๊ฐ์๋ฅผ ๋น๊ตํด ๊ฐ์ผ๋ฉด True, ๋ค๋ฅด๋ฉด False๋ฅผ return ํ๋ solution๋ฅผ ์์ฑํ์ธ์. 'p', 'y' ๋ชจ๋ ํ๋๋ ์๋ ๊ฒฝ์ฐ๋ ํญ์ True๋ฅผ
programmers.co.kr
์ฝ๋
#include <string>
#include <iostream>
using namespace std;
bool solution(string s)
{
bool answer = true;
int p_cnt=0,y_cnt=0;
for(int i=0; i<s.length(); i++){
if(s[i]=='p'||s[i]=='P') p_cnt++;
else if(s[i]=='y'||s[i]=='Y') y_cnt++;
}
if(p_cnt==y_cnt) answer=true;
else answer=false;
return answer;
}