문자열 내 p와 y의 개수

문제 설명

대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를 리턴합니다. 단, 개수를 비교할 때 대문자와 소문자는 구별하지 않습니다.

예를 들어 s가 pPoooyY면 true를 return하고 Pyy라면 false를 return합니다.

 

제한 사항

  • 문자열 s의 길이 : 50 이하의 자연수
  • 문자열 s는 알파벳으로만 이루어져 있습니다.

 

입출력 예

s answer
pPoooyY true
Pyy false

 

Python

def solution(s):
    S = s.upper();
    num = 0;
    for i in S:
        if i == 'P':
            num += 1
        elif i== 'Y':
            num += -1
    return num == 0 and True or False

 

JavaScript

JavaScript toUpperCase

 

String.prototype.toUpperCase() - JavaScript | MDN

toUpperCase() 메서드는 문자열을 대문자로 변환해 반환합니다. The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/

developer.mozilla.org

function solution(s){
    var num = 0;
    s.toUpperCase();
    for(let i = 0; i < s.length; i++){
        if(s[i] === 'P') num++;
        else if(s[i] === 'Y') num--;
    }
    
    return num === 0 ? true : false;
}

 

C++

Convert a String In C++ To Upper Case

 

Convert a String In C++ To Upper Case

How could one convert a string to upper case. The examples I have found from googling only have to deal with chars.

stackoverflow.com

#include <string>
#include <iostream>
#include <boost/algorithm/string.hpp>
using namespace std;

bool solution(string s)
{
    int num = 0;
    boost::to_upper(s);
    for(int i = 0; i < s.size(); i++){
        if(s[i] == 'P') num++;
        else if(s[i] == 'Y') num--;
    }
    return num == 0 ? true : false;
}

 

  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기