[Algorithm] 45 week(12.05 ~ 12.11) 2/3

Dev_min·2022년 12월 6일
0

algorithm

목록 보기
145/157

17. Letter Combinations of a Phone Number

var letterCombinations = function(digits) {
    const dic = { "2":"abc", "3":"def", "4":"ghi", "5":"jkl", "6":"mno", "7":"pqrs", "8":"tuv", "9":"wxyz"}
    const result = [];

    if(digits.length === 0) return [];

    const dfs = (index, path) => {
        if(index === digits.length){
            result.push(path);
            return;
        }

        let word = dic[digits[index]];

        for(let j = 0; j < word.length; j++){
            dfs(index + 1, path + word[j]);
        }
    }


    dfs(0, '');

    return result;
};
profile
TIL record

0개의 댓글