https://programmers.co.kr/learn/courses/30/lessons/12916

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <iostream>
using namespace std;
 
bool solution(string s)
{
    bool answer = false;
    int pQuantity = 0;
    int yQuantity = 0;
    
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == 'P' || s[i] == 'p')
        {
            pQuantity++;
            continue;
        }
        if (s[i] == 'Y' || s[i] == 'y')
        {
            yQuantity++;
            continue;
        }
    }
    if (pQuantity == yQuantity)answer = true;
 
    return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

문자열의 크기 만큼 하나씩 검사하고,

해당 문자가 있으면 해당되는 int 변수를 ++한다.

for문이 완료된 후 int변수의 크기를 비교하고 리턴하면 된다.

+ Recent posts