Python Solution


  • 0
    V
    def letterCombinations(self, digits):
        final=[]
        letters_mapping = {'2':'abc','3':'def','4':'ghi','5':'jkl','6':'mno','7':'pqrs','8':'tuv','9':'wxyz'}
        if len(digits) == 0:
            return final
        if len(digits) == 1:
            dig = letters_mapping[digits]
            for i in range(len(dig)):
                final.append(dig[i])
            return final
        else:
            temp0 =[]
            dig0 = letters_mapping[digits[0]]
            for i in range(len(dig0)):
                temp0.append(dig0[i])
            for rest in self.letterCombinations(digits[1:]):
                final.extend([i+rest for i in temp0])
            return final

Log in to reply
 

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