대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를 리턴합니다. 단, 개수를 비교할 때 대문자와 소문자는 구별하지 않습니다.
예를 들어 s가 "pPoooyY"면 true를 return하고 "Pyy"라면 false를 return합니다.
s | answer |
---|---|
"pPoooyY" | true |
"Pyy" | false |
입출력 예 #1
입출력 예 #2
function solution(s){
var answer = true;
// [실행] 버튼을 누르면 출력 값을 볼 수 있습니다.
console.log('Hello Javascript')
return answer;
}
//왜 안되는지 모르겠지만 30tests 중 4tests에서 run time error가 나온 풀이
function solution(s){
return s.match(/p/gi).length == s.match(/y/gi).length;
}
//split으로 바꿔서 해결
function solution(s){
return s.split(/p/gi).length - 1 == s.split(/y/gi).length - 1;
//예시 1번의 s를 console.log(s.split(/p/gi))를 했을 경우, ['','','oooyY'] 이렇게 나오므로 전체 length에서 1을 빼주었다.
}
.match(RegExp)를 사용해서 푸려고 했지만 runtime error가 자꾸 발생했다. 왜지... 그래서 다른 함수 .split을 찾아서 해결했다
(출처: https://developer.mozilla.org/ko/)
//첫 번째 풀이
function numPY(s){
return s.toUpperCase().split("P").length === s.toUpperCase().split("Y").length;
}
//두 번째 풀이 = 내 첫 번째 풀이 ㅋㅋ 런타임 에러남~
function numPY(s) {
return s.match(/p/ig).length == s.match(/y/ig).length;
}
(출처: https://developer.mozilla.org/ko/)