Functional programming way--reduce


  • 1
    K

    Map and Reduce in Python make it beautiful sometimes.

    z represents partial result

    x represents digits list

    class Solution:
      def letterCombinations(self, digits):
        theGrid = ["", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
        f = lambda l: reduce(lambda z, x: [y + p for p in theGrid[int(x) - 1] for y in z], l, [""])
        return f(digits) if digits else []

Log in to reply
 

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