재귀를 돌면서, cnt++로 순서를 추적했다.
tmp.join('') 값과 주어진 word 값이 같으면 답을 도출하고 return한다.
function solution(word) {
const apb = ['A', 'E', 'I', 'O', 'U'];
let tmp = [];
let cnt = 0;
let answer = 0;
function dict(L) {
cnt++;
// console.log(tmp, cnt);
if (tmp.join('') === word) {
answer=cnt-1;
return;
}
if (L>=apb.length) return;
else {
for (let i=0; i<apb.length; i++) {
tmp.push(apb[i]);
dict(L+1);
tmp.pop();
}
}
}
dict(0);
return answer;
}
console.log(solution("AAAAE"));
[프로그래머스] 모음 사전 js 풀이
[프로그래머스] 모음사전 Javascript
아래와 같은 규칙 발견하기
const plus = [?, ?, ?, 1*5+1, 1]; const plus = [?, ?, 6*5+1, 6, 1]; const plus = [?, 31*5+1, 31, 6, 1]; const plus = [156*5+1, 156, 31, 6, 1]; const plus = [781, 156, 31, 6, 1];
function solution(word) {
const obj = {
A: 0,
E: 1,
I: 2,
O: 3,
U: 4,
};
const plus = [781, 156, 31, 6, 1];
return word
.split("")
.reduce((acc, ch, idx) => acc + obj[ch] * plus[idx] + 1, 0);
}
console.log(solution("EIO"));
등비수열의 합?
function solution(word) {
return word.split('')
.reduce((a,b,i) => a + ('AEIOU'.indexOf(b)) * (5**(5 - i) - 1) / 4 + 1, 0)
}