[Lv1] 문자열 내 p와 y의 개수

Creating the dots·2021년 12월 21일
0

Algorithm

목록 보기
38/65

프로그래머스

나의 풀이

  • 반복문을 사용해서 시간이 다른 풀이에 비해 조금 오래걸림.
function solution(s){
  let p=0;
  let y=0;
  for(let i=0; i<s.length; i++){
    if(s[i]==='p' || s[i]==='P') p++;
    if(s[i]==='y' || s[i]==='Y') y++;
  }
  return p===y;
}

다른 풀이

function solution(s){
  return s.toUpperCase().split("P").length === s.toUpperCase().split("Y").length;
}

const str = 'ssppPPyYyy';
str.toUpperCase().split('P'); //["SS", "", "", "", "YYYY"]
str.toUpperCase().split('Y'); //["SSPPPP", "", "", "", ""]
profile
어제보다 나은 오늘을 만드는 중

0개의 댓글