fork download
  1. # Python program to count decoding ways of a digit string
  2. # using Tabulation
  3.  
  4. # Function to count decoding ways for the entire string.
  5. def countWays(digits):
  6. n = len(digits)
  7.  
  8. # Create a dp list initialized to 0, with size n + 1.
  9. dp = [0] * (n + 1)
  10.  
  11. # Base case: An empty string has one valid decoding.
  12. dp[n] = 1
  13.  
  14. # Iterate from the end of the string to the beginning.
  15. for i in range(n - 1, -1, -1):
  16.  
  17. # Single-digit decoding: check if current
  18. # digit is not '0'.
  19. if digits[i] != '0':
  20. dp[i] = dp[i + 1]
  21.  
  22. # Two-digit decoding: check if next two digits are valid.
  23. if (i + 1 < n and
  24. ((digits[i] == '1' and digits[i + 1] <= '9') or
  25. (digits[i] == '2' and digits[i + 1] <= '6'))):
  26. dp[i] += dp[i + 2]
  27.  
  28. return dp[0]
  29. if __name__ == "__main__":
  30. digits = "1212323223232323232323233223322322222222222222222222222211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111121232322323232323232323322332232222222222222222222222221111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112123232232323232323232332233223222222222222222222222222111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111212323223232323232323233223322322222222222222222222222211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111121232322323232323232323322332232222222222222222222222221111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112123232232323232323232332233223222222222222222222222222111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111212323223232323232323233223322322222222222222222222222211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111121232322323232323232323322332232222222222222222222222221111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112123232232323232323232332233223222222222222222222222222111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111212323223232323232323233223322322222222222222222222222211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111121232322323232323232323322332232222222222222222222222221111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112123232232323232323232332233223222222222222222222222222111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111212323223232323232323233223322322222222222222222222222211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111121232322323232323232323322332232222222222222222222222221111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112123232232323232323232332233223222222222222222222222222111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111212323223232323232323233223322322222222222222222222222211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111121232322323232323232323322332232222222222222222222222221111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112123232232323232323232332233223222222222222222222222222111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111212323223232323232323233223322322222222222222222222222211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111121232322323232323232323322332232222222222222222222222221111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112123232232323232323232332233223222222222222222222222222111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111212323223232323232323233223322322222222222222222222222211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111121232322323232323232323322332232222222222222222222222221111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112123232232323232323232332233223222222222222222222222222111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111212323223232323232323233223322322222222222222222222222211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111121232322323232323232323322332232222222222222222222222221111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
  31. print(countWays(digits))
Success #stdin #stdout 0.14s 14608KB
stdin
Standard input is empty
stdout
8801473530721878707080545098338582804982879765207832887064896248404794556367257896325842042481474319515612597339005051410338130646936890932024209593070059869642298615318475070766516370439397128140871148979590561380459214273142259849487983804598672131362383242117878720700010136970706771463352712237755458451638221925494757541940240443331225350498820201537692497200925249199907476712779864540965866804025440904001728637923136789834458433654151277388061808598754457947090267939848110239464708727134779150121775687434331266020782285787368207844532895249884820202096511400906766467747842623770249024651251981933385790318888973430874188160956104315162560348718719306938892680690327323975223378122125283111587640328099428355211475716726011030246072780866709778790382100271164978941509539093640763786162225236844106019289930369564264764009518200444400611296948998501747082746710413646193740556360333740429848264787894295809022847691196199906048943454829989601263412506569269847021646414548655851337590821027064573918481140826362352955670398622817659264424595442740552203903258608949804012217919292629265880432299901834346116848797290881253185511348379953188174643377436478216976173217695731497888622523202704741836503116540155953142247427012639599489635523781781482125907557879442692574933862557615981992090916924061988391352733345528700933365147193842252230105159153537999322401672733676725090996979309853713747596214416763875768579010762637312