머쓱이는 태어난 지 6개월 된 조카를 돌보고 있습니다. 조카는 아직 "aya", "ye", "woo", "ma" 네 가지 발음을 최대 한 번씩 사용해 조합한(이어 붙인) 발음밖에 하지 못합니다. 문자열 배열 babbling
이 매개변수로 주어질 때, 머쓱이의 조카가 발음할 수 있는 단어의 개수를 return하도록 solution 함수를 완성해주세요.
babbling
의 길이 ≤ 100babbling[i]
의 길이 ≤ 15babbling
의 각 문자열에서 "aya", "ye", "woo", "ma"는 각각 최대 한 번씩만 등장합니다.
babbling | result |
---|---|
["aya", "yee", "u", "maa", "wyeoo"] | 1 |
["ayaye", "uuuma", "ye", "yemawoo", "ayaa"] | 3 |
입출력 예 #1
입출력 예 #2
※ 공지 - 2022년 10월 27일 문제 지문이 리뉴얼되었습니다. 기존에 제출한 코드가 통과하지 못할 수도 있습니다.
function solution(babbling) {
return babbling.map(e=>e.replaceAll('aya',' '))
.map(e=>e.replaceAll('ye',' '))
.map(e=>e.replaceAll('woo',' '))
.map(e=>e.replaceAll('ma',' ').split(' ').join(''))
.filter(e=>!e).length;
}
function solution(babbling) {
return ['aya','ye','woo','ma'].reduce((a,c)=>a.map(e=>e.replaceAll(c,' ')),babbling).map(e=>e.split(' ').join('')).filter(e=>!e).length;
}
function solution(babbling) {
return babbling.map(e=>e.split('aya'))
.map(e=>e.map(i=>i.split('ye')).flat())
.map(e=>e.map(i=>i.split('woo')).flat())
.map(e=>e.map(i=>i.split('ma')).flat().join(''))
.filter(e=>!e).length
}
다른풀이
function solution(babbling) {
var answer = 0;
const regex = /^(aya|ye|woo|ma)+$/;
babbling.forEach(word => {
if (regex.test(word)) answer++;
})
return answer;
}