1 line ugly python solution and a 3-line beautiful one


  • 0
    M

    Although ugly, it's not difficult to understand:

    class Solution:
        # @param {string} digits
        # @return {string[]}
        def letterCombinations(self, digits):
            return [''.join(i) for i in itertools.product(*map(lambda x: [" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"][int(x)], digits))] if digits else []
    

    In fact, It's equivalent to the following beautiful 3-line one:

    class Solution:
        # @param {string} digits
        # @return {string[]}
        def letterCombinations(self, digits):
            mapper = [" ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
            graph = map(lambda x: mapper[ord(x) - ord('0')], digits)
            return [''.join(i) for i in itertools.product(*graph)] if digits else []

Log in to reply
 

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