4 lines simple Python


  • 0

    Just use a helper function to get all possible results.

    class Solution(object):
        def letterCombinations(self, digits):
            """
            :type digits: str
            :rtype: List[str]
            """
            dic = {"2":"abc", "3":"def", "4":"ghi", "5":"jkl", \
                   "6":"mno", "7":"pqrs", "8":"tuv", "9":"wxyz"}
            def helper(d):
                return [x+y for x in dic[d[0]] for y in helper(d[1:])] if d else [""]
            return helper(digits) if digits else []
    

Log in to reply
 

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