Very straight-forward python code


  • 0
    J
    class Solution:
        # @return a list of strings, [s1, s2]
    
        '''
        helper function:
        @param: the exisiting list of letter Combinations
        @param: the new possibilities
        '''
        def generateRes(self, res, letters):
        	temp = []
        	for letter in letters:
        		if res == []:
        			temp.append(letter)
        		for result in res:
        			temp.append(result+letter)
        	return temp
    
        def letterCombinations(self, digits):
        	res = [""]
            for d in digits:
            	if int(d) == 2:
            		res = self.generateRes(res,['a','b','c'])
            	elif int(d) == 3:
            		res = self.generateRes(res,['d','e','f'])
            	elif int(d) == 4:
            		res = self.generateRes(res,['g','h','i'])
            	elif int(d) == 5:
            		res = self.generateRes(res,['j','k','l'])
            	elif int(d) == 6:
            		res = self.generateRes(res,['m','n','o'])
            	elif int(d) == 7:
            		res = self.generateRes(res,['p','q','r','s'])
            	elif int(d) == 8:
            		res = self.generateRes(res,['t','u','v'])
            	elif int(d) == 9:
            		res = self.generateRes(res,['w','x','y','z'])
            return res

Log in to reply
 

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