a simple python solution


  • 0
    N
    class Solution(object):
        def letterCombinations(self, digits):
            """
            :type digits: str
            :rtype: List[str]
            """
            digits_dict = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
            
            ret_list = []
            if not digits or len(digits) == 0:
                return ret_list
            
            first_digit = digits[0]
            for c in digits_dict[first_digit]:
                ret_list.append(c)
            
            for i in xrange(1, len(digits)):
                new_list = []
                digit_str = digits_dict[digits[i]]
                for c in digit_str:
                    for s in ret_list:
                        new_list.append(s + c)
                ret_list = new_list
            
            return ret_list
    

Log in to reply
 

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