Python iteration solution fast and clear


  • 0
    B

    '''

    class Solution(object):

    def letterCombinations(self, digits):
    
        if len(digits) == 0:
            return []
    
        mapping = ['0', '1', 'abc', 'def', "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
        output = []
        
        for num in digits:
            idx = ord(num) - ord('0')
            if len(output) == 0:
                for char in mapping[idx]:
                    output.append(char)
            else:
                new_output = []
                for i in range(len(output)):
                    result = output[i]
                    for char in mapping[idx]:
                        new_output.append(result + char)
                output = new_output
                
        return output
    

    '''


Log in to reply
 

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