프로그래머스 Lv.1 : 문자열 내 p와 y의 개수

zeroequaltwo·2022년 11월 16일
0

코딩테스트

목록 보기
7/69

문제

프로그래머스 문제

내 풀이

function solution(s){
    const pLength = s.split('p').length - 1 + s.split('P').length - 1;
    const yLength = s.split('y').length - 1 + s.split('Y').length - 1;

    return pLength === yLength ? true : false;
}

개선점

  • 바보같이 대소문자 상관없이를 저렇게 썼다. toUpperCase()나 toLowerCase()를 썼으면 저렇게 split 2번씩이나 할 필요가 없어진다.
  • match로 푼 사람의 풀이법도 갖고왔다. match는 정규식을 알면 더 쓰기 좋다.
function numPY(s) {
  return s.match(/p/ig).length == s.match(/y/ig).length;
}
profile
나로 인해 0=2가 성립한다.

0개의 댓글