Python solution


  • 0
    D
        def letterCombinations(self, digits):
            d = {"1":"","2":"abc","3":"def", "5":"jkl","4":"ghi","6":"mno","7":"pqrs","8":"tuv","9":"wxyz","0":""}
            l = {"1":0,"2":3,"3":3, "4":3,"5":3,"6":3,"7":4,"8":3,"9":4,"0":0}
    
            n = len(digits)
            if not n:
                return []
            
            iter_cum = [1]*n
            iters = 1
            for c in range(n):
                iter_cum[c] = iters
                iters *= l[digits[c]]
    
            res = [""]*iters
            for i in range(iters):
                s = ""
                for j in range(n):
                    s += d[digits[j]][(i//iter_cum[j])%l[digits[j]]]
                res[i] = s
            return res
    

Log in to reply
 

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