I'm proud of my list comprehension here!

  • 1
    class Solution:
    def letterCombinations(self, digits):
        theGrid = ["","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"];
        theList = [""];
        for i in digits:
            theList = [x+y for x in theList for y in theGrid[int(i)-1]]
        return theList;

  • 2

    No need to proud, it's well know that Python list comprehension is good for one-liner BFS.

    class Solution:
        # @return a list of strings, [s1, s2]
        def letterCombinations(self, digits):
            mappings = ['0', '1', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
            results = ['']
            for i in digits:
                results = [ancestor+c for ancestor in results for c in mappings[int(i)]]
            return results

Log in to reply

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