[python] CodeForLetterComb

Viewer

copydownloadembedprintName: CodeForLetterComb
  1. class Solution:
  2.     def letterCombinations(self, digits: str) -> List[str]:
  3.         two = ['a','b','c']
  4.         three = ['d','e','f']
  5.         four = ['g','h','i']
  6.         five = ['j','k','l']
  7.         six = ['m','n','o']
  8.         seven = ['p','q','r','s']
  9.         eight = ['t','u','v']
  10.         nine = ['w','x','y','z']
  11.         
  12.         if len(digits) == 0:
  13.             return []
  14.  
  15.         outputList = []
  16.         twoDList = []
  17.  
  18.         total = 1
  19.  
  20.         for x in digits:
  21.             if int(x) == 2:
  22.                 twoDList.append(two)
  23.                 total *= 3
  24.             elif int(x) == 3:
  25.                 twoDList.append(three)
  26.                 total *= 3
  27.             elif int(x) == 4:
  28.                 twoDList.append(four)
  29.                 total *= 3
  30.             elif int(x) == 5:
  31.                 twoDList.append(five)
  32.                 total *= 3
  33.             elif int(x) == 6:
  34.                 twoDList.append(six)
  35.                 total *= 3
  36.             elif int(x) == 7:
  37.                 twoDList.append(seven)
  38.                 total *= 4
  39.             elif int(x) == 8:
  40.                 twoDList.append(eight)
  41.                 total *= 3
  42.             elif int(x) == 9:
  43.                 twoDList.append(nine)
  44.                 total *= 4
  45.  
  46.         cycle = total
  47.         for x in range(0,len(digits)):
  48.             cycle = cycle/len(twoDList[x])
  49.             inc = 0
  50.             digit = 0
  51.             for y in range(0, total):
  52.                 if len(outputList) == total:
  53.                     outputList[y] += twoDList[x][digit]
  54.                 else:
  55.                     outputList.append(twoDList[x][digit])
  56.                 if inc == (cycle-1):
  57.                     inc = 0
  58.                     if digit == (len(twoDList[x])-1):
  59.                         digit = 0
  60.                     else:
  61.                         digit += 1
  62.                 else:
  63.                     inc += 1
  64.  
  65.         return outputList
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.         
  74.         

Editor

You can edit this paste and save as new:


File Description
  • CodeForLetterComb
  • Paste Code
  • 13 Jun-2021
  • 1.94 Kb
You can Share it: