티스토리 뷰

 

 

 

문제 설명

 

대문자와 소문자가 섞여있는 문자열 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

 

 

 

 

Solution in Java

public class Main {
 
    public static void main(String[] args) {
        String s = "yypypypppy";
        Solution sol = new Solution();
        boolean answer = sol.solution(s);
        System.out.println(answer);    }
}
 
class Solution {
    boolean solution(String s) {
        boolean answer = true;
        int count = 0;
        int count2 = 0;
        s = s.toLowerCase();
 
        for(int i = 0;i<s.length(); i++ ){
            if(s.charAt(i) == 'y')
            {count++;}
            //단순히 int variable 만 for loop로 카운트 올려서 비교로 사용!
            if(s.charAt(i) == 'p')
            {count2++;}
        }
        if(count == count2){
            answer = true;
        }else{
            answer = false;
        }
 
        return answer;
    }
}
 
cs

 

Solution in JavaScript


let answer;

function Solution(prop) {
    let p = 0;
    let y = 0;
    let str = prop.toLowerCase();
    for( let i = 0; i < str.length; i++){
        if(str.charAt(i) === 'p') {
            p++;
        } else if (str.charAt(i) === 'y'){
            y++;
        }
    }
    if(p == y) {
        return answer = true;
    } else {
        return answer = false;
    }
}

console.log(Solution("yyPypyppp")); // false

 

 

 

 

 

 

reference 

https://programmers.co.kr