Resursive python


  • 0
    A

    class Solution(object):
    def letterCombinations(self, digits):
    """
    :type digits: str
    :rtype: List[str]
    """
    if len(digits) == 0: return []

        def getresult(digits):
          if len(digits) == 0:
            return []
          if len(digits) == 1:
            return [d for d in mapping[digits[0]]]
          prevresults = get(digits[:len(digits)-1])
          result = []
          for v in mapping[digits[-1]]:
            for prevresult in prevresults:
              result.append(prevresult+v)
          return result
    
        mapping = {'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']}
        return getresult(digits)

Log in to reply
 

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