Python Recursive Solution


  • 0
    R
    class S(object):
    	def __init__(self):
    		self.sol = [] 
    		self.map = {}
    		self.map['2'] = ["a","b","c"]
    		self.map['3'] = ["d","e","f"]
    		self.map['4'] = ["g","h","i"]
    		self.map['5'] = ["j","k","l"]
    		self.map['6'] = ["m","n","o"]
    		self.map['7'] = ["p","q","r","s"]
    		self.map['8'] = ["t","u","v"]
    		self.map['9'] = ["w","x","y","z"]
    
    	# Minha Solucao
    	def phonePhrase(self, num, n, string):
    		if(len(num) == 0):
    			return
    		if(n == len(num)):
    			self.sol.append(string)
    			return
    		l = self.map[num[n]]
    		for i in l:
    			self.phonePhrase(num, n+1, string+i)
    
    class Solution(object):
        def letterCombinations(self, digits):
            sol = S()
            sol.phonePhrase(digits, 0, '')
            return sol.sol
    ´´´

Log in to reply
 

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