주어진 숫자 다이얼을 이용하여 아래 키패드로 나올수 있는 문자 조합을 모두 구하여하려.
/**
* @param {string} digits
* @return {string[]}
*/
var letterCombinations = function (digits) {
const phoneKeypad = {
2: ['a', 'b', 'c'],
3: ['d', 'e', 'f'],
4: ['g', 'h', 'i'],
5: ['j', 'k', 'l'],
6: ['m', 'n', 'o'],
7: ['p', 'q', 'r', 's'],
8: ['t', 'u', 'v'],
9: ['w', 'x', 'y', 'z'],
}
let answer = []
const combinationStr = (digits, deep, strs) => {
if (deep === digits.length) {
strs.join() && answer.push(strs.join(''))
return
}
for (const d of phoneKeypad[digits[deep]]) {
strs[deep] = d
combinationStr(digits, deep + 1, strs)
}
}
combinationStr(digits, 0, Array(digits.length))
return answer
}