Simple Python solution


  • 0
    K
    class Solution(object):
        def letterCombinations(self, digits):
            """
            :type digits: str
            :rtype: List[str]
            """
            if not digits: return []
            num_dict = {
                '1': '*',
                '2': 'abc', 
                '3': 'edf',
                '4': 'ghi',
                '5': 'jkl',
                '6': 'mno',
                '7': 'pqrs',
                '8': 'tuv',
                '9': 'wxyz',
                '0': ' '
            }
            res = ['']
            for d in digits:
                new_res = []
                for comb in res:
                    for v in num_dict[d]:
                        new_res.append(comb + v)
                res = new_res
            return res
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.