프로그래머스(Programmers)

Level 1> 문자열 내 p와 y의 개수

cepiloth 2018. 8. 8. 15:07
반응형

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


1. 문제


2. 알고리즘

키워드 - 구현, 문자열


3. 코드


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
28
29
30
#include <string>
#include <iostream>
#include <algorithm>
#include <locale>
 
using namespace std;
 
bool solution(string s)
{
    bool answer = true;
    const int size = s.size();
    
    int count_p = 0;
    int count_y = 0;
    
    transform(s.begin(), s.end(), s.begin(), ::tolower);
    
    for(int i=0; i<size; i++) {
        if(s[i] == 'y')
            count_y++;
        
        if(s[i] == 'p')
            count_p++;
    }
    
    if(count_p != count_y)
        answer = false;
    
    return answer;
}
cs

반응형