알고리즘 공부 2020. 8. 20. 22:38

https://leetcode.com/problems/letter-combinations-of-a-phone-number/

 

Letter Combinations of a Phone Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

/**
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function(digits) {
    if( digits === "" )
        return [];
    
    const num_string = { '2':'abc', '3':'def', '4':'ghi','5':'jkl','6':'mno','7':'pqrs','8':'tuv','9':'wxyz' };
    
    const res = [];    
    const find = function( p_num_idx, p_str )
    {        
        if( p_str.length === digits.length )        
            res.push( p_str )        
        else
        {
            const str = num_string[ digits[p_num_idx] ];
            for( let i=0; i<str.length; ++i )            
                find( p_num_idx+1, p_str.concat(str[i]) );            
        }
    }
    find(0,'');
    
    return res;
};
posted by Sense.J
: