문제 :
대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를 리턴합니다. 단, 개수를 비교할 때 대문자와 소문자는 구별하지 않습니다.
#include
#include
#include
using namespace std;
bool solution(string s)
{
bool answer = true;
int countP = 0;
int countY = 0;
string str;
for (int i = 0; i < s.size(); i++)
{
str = s.at(i);
if (str == (string)"P" || str == (string)"p")
{
countP += 1;
}
else if (str == (string)"Y" || str == (string)"y")
{
countY += 1;
}
}
if (countP != countY)
{
answer = false;
}
return answer;
}
'알고리즘 문제' 카테고리의 다른 글
(C++) 2016년 (0) | 2020.05.07 |
---|---|
(C++)가운데 글자 가져오기 (0) | 2020.05.05 |
(C++)나누어 떨어지는 숫자 배열 (0) | 2020.05.04 |
(C++)두 정수 사이의 합 (0) | 2020.05.02 |
(C++) K번째 수 (0) | 2020.04.24 |