문제 🔽
https://school.programmers.co.kr/learn/courses/30/lessons/84512
다른사람 풀이 🔽
const VOWELS = ['A','E','I','O','U','']
const f=(a)=>VOWELS.map(b=>b+a)
function solution(word) {
return Array.from(
new Set(VOWELS
.map(f).flat()
.map(f).flat()
.map(f).flat()
.map(f).flat())
).sort().indexOf(word)
}
내풀이🔽
1. 성공 ⭕
function solution(word) {
let charArr = ["A","E","I","O","U"]
let t = []
var answer = 0;
const dfs=(cha)=>{
if(cha.length>5){ return }
t.push(cha);
for(let i=0; i<5; i++){
dfs(cha+charArr[i])
}
}
for(let a of charArr){
dfs(a)
}
t.sort((a,b)=> b-a)
answer=1+t.indexOf(word)
return answer;
}
느낀점🔽
알파벳으로 만들 수 있는 모든 경우의 수는 3,125 개 이므로 dfs를 이용해도 시간은 충분하다는 걸 인지한 순간 풀이가 쉬어 졌다.
'Javascript' 카테고리의 다른 글
프로그래머스 코딩테스트 풀이(js) > 같은 숫자는 싫어(lv1) (0) | 2023.08.02 |
---|---|
프로그래머스 코딩테스트 풀이(js) > 문자열 내 마음대로 정렬하(lv1) (0) | 2023.08.01 |
프로그래머스 코딩테스트 풀이(js) > [1차]캐시(lv2) (0) | 2023.07.30 |
프로그래머스 코딩테스트 풀이(js) > 최소직사각형(lv1) (0) | 2023.07.29 |
프로그래머스 코딩테스트 풀이(js) > k번째수 (lv1) (0) | 2023.07.27 |