프로그래머스[문자열 p와 y의 개수]

이유정·2022년 10월 16일
0

문제 설명

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

입출력 예 설명

입출력 예 #1
'p'의 개수 2개, 'y'의 개수 2개로 같으므로 true를 return 합니다.

입출력 예 #2
'p'의 개수 1개, 'y'의 개수 2개로 다르므로 false를 return 합니다.

※ 공지 - 2021년 8월 23일 테스트케이스가 추가되었습니다.


내가 실패한 코드

function solution(s){
 let pcount = 0; 
 let ycount = 0;
    for(let i=0; i<s.length; i++){
        if(s[i] === 'p' || 'P') pcount++;
        if(s[i] === 'y' || 'Y') ycount++;   
    }
    if(pcount === ycount) {
        return true;
    } return false; 
}

디버거를 실행해보니, pcount++와 ycount++가 제대로 작동되지 않음을 알 수 있다. (왜 안되죠? ㅠㅠ?)

결론: 저 조건 부분을 잘못 설정했기 때문이다 !

이걸 알아내기 위해서 이 블로그 글 맨 밑에 수많은 과정을 거쳤다... 힘들었다.ㅋㅋㅋㅋㅋ

function solution(s){
 let pcount = 0; 
 let ycount = 0;
    for(let i=0; i<s.length; i++){
        if(s[i] === 'p' || s[i] === 'P') pcount++;
        if(s[i] === 'y' || s[i] === 'Y') ycount++;   
    }
    if(pcount === ycount) {
        return true;
    } return false; 
}

###다른 사람 풀이 코드1

function solution(s){
 let string = s.toLowerCase()
 let split_p = string.split('p').length;
 let split_y = string.split('y').length;
 console.log(split_y)
    if(split_p === split_y) return true; 
    return false; 
}

###다른 사람 풀이 코드2 (1꺼 정리한 코드)

function numPY(s){
  //함수를 완성하세요
    return s.toUpperCase().split("P").length === s.toUpperCase().split("Y").length;
}

###다른 사람 풀이 코드3

function numPY(s) {
  return s.match(/p/ig).length == s.match(/y/ig).length;
}

###다른 사람 풀이 코드4

function numPY(s){
  var result = true;
  s = s.toUpperCase();
  var num = 0;
  for(var i = 0; i < s.length; i++){
    if(s[i] === 'P') num++;
    if(s[i] === 'Y') num--;
  }
  result = (num === 0);

  return result;
}

틀린 내 코드 고쳐보는 과정

과정1)

// 저 p와 y부분을 괄호로 감싸줘봤다. 그래도 틀림

function solution(s){
 let pcount = 0; 
 let ycount = 0;
    let result = true; 
    for(let i=0; i<s.length; i++){
        if(s[i] === ('p' || 'P')) pcount++;
        if(s[i] === ('y' || 'Y')) ycount++;   
    }
    result = (pcount === ycount)
    return result; 
}

과정2)

위 코드에서 밑에 if 부분을 바꿔봤다.
이것도 틀림

function solution(s){
 let pcount = 0; 
 let ycount = 0;
    let result = true; 
    for(let i=0; i<s.length; i++){
        if(s[i] === ('p' || 'P')) pcount++;
        if(s[i] === ('y' || 'Y')) ycount++;   
    }
    if(pcount === ycount){
        return true;
    }else{
        return false; 
    }
}

과정3)

이것도 안된다 . 아마 그냥 p와 y 그부분을 함수를 이용해서 대문자나 소문자로 싹 바꿔준다음에 num을 다뤄야겠다. 그러면 성공

function solution(s){
let num = 0
    let result = true; 
    for(let i=0; i<s.length; i++){
        if(s[i] === ('p' || 'P')) num++
        if(s[i] === ('y' || 'Y')) num--;   
    }
    result = (num === 0)
    return result; 
}

과정4)
갑자기 스쳐가는 생각!!
아 그 || 이거 써서 코드 작성하려면 이렇게 나눠줘야 한다.
흑 아마 이거때문에 내 코드가 말썽이였던 것 같다.

function solution(s){
let num = 0
    let result = true; 
    for(let i=0; i<s.length; i++){
        if(s[i] === 'P' || s[i] === 'p') num++
        if(s[i] === 'Y' || s[i] === 'y') num--;   
    }
    result = (num === 0)
    return result; 
}

결론)

와 드디어 알아냈다

내 코드가 왜 틀렸는지

이렇게 하면 성공이다 !!!

function solution(s){
 let pcount = 0; 
 let ycount = 0;
    for(let i=0; i<s.length; i++){
        if(s[i] === 'p' || s[i] === 'P') pcount++;
        if(s[i] === 'y' || s[i] === 'Y') ycount++;   
    }
    if(pcount === ycount) {
        return true;
    } return false; 
}
profile
팀에 기여하고, 개발자 생태계에 기여하는 엔지니어로

0개의 댓글