Letter Combinations of a Phone Number


  • 0
    N
    def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """
        if digits =='':
            return []
        mapping = {'1':[''],
             '2':['a','b','c'],
             '3':['d','e','f'],
             '4':['g','h','i'],
             '5':['j','k','l'],
             '6':['m','n','o'],
             '7':['p','q','r','s'],
             '8':['t','u','v'],
             '9':['w','x','y','z'],
             '0':[' ']}
        res =['']
        
        for d in digits:
            temp = []
            for item in res:
                for x in mapping[d]:
                    temp.append(item+x)
            res = temp
            
        return res

Log in to reply
 

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