Python Solution Easy to Understand


  • 1
    P
    class Solution(object):
    	def letterCombinations(self, digits):
    		"""
    		:type digits: str
    		:rtype: List[str]
    		"""
    		if digits == "":
    			return []
    		dic =  {"2": "abc", "3": "def", "4": "ghi", "5": "jkl", "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz"}
    		path = ""
    		res = []
    		def travel( digits, path, res):
    			if digits == "":
    				res.append( path )
    				return 
    			for i in dic[digits[0]]:
    				travel( digits[1:], path+i, res )
    			return
    		travel(digits, path, res)
    		return res
    

  • 0
    L

    Great solution, but string concatenation is very costly


Log in to reply
 

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