#
# 解码
# @param nums string字符串 数字串
# @return int整型
#
class Solution:
def solve(self , nums ):
# write code here
#
# state dp[i] = number of decoding ways
# base case "n", if n ==0, return 0, if "n" >0 and <9 return 1
# "nm": if int(nm) =0 return 0 and if 0<int(nm) < 11, return 1 "m"
# if int(nm)<=26: return 2 "n/m" and "nm"
#
# "nmk": consider one way separate k with previous char
#: "nm/k" then this is equal to dp[i-1] decoding ways
# consider "n/mk", combine previous char with k and think mk as one char
# then we always let n, mk separate, then our solution is
# dp[i-2] = solve("n")
#
# because we can only combine two digiit at max, so no need to think
# the previous cases
#
# so if "n/mk", int("mk")<26, then dp[i] = dp[i-1] + dp[i-2]
# if int("mk") >26 or int("mk")==0, dp[i] = dp[i-1]
# if "nm/k" int("k") ==0, then this way is invalid, otherwise,
# dp[i] += dp[i-1]
# if "n/mk", int("mk") ==0 or int("mk") >26, then this way is invalid
# otherwise: dp[i] += dp[i-2]
# if two cases exist, then dp[i] = 0
#
# test case: "31" -> "3/1", "/31"
#
#
#
if nums == "0":
return 0
if len(nums) ==1:
return 1
dp =[0]*len(nums)
for i in range( len(nums)):
if i ==0:
if nums[i] != "0":
dp[i] = 1
else:
# consider the case that we separate the last digit
# in this way "..../k"
# if "k" >0 and <=9 then we add previous solution dp[i-1]
if i-1 >=-1 and int(nums[i])>0:
dp[i] += dp[i-1]
# consider the case we separate the digit in this way
# ".../mk"
# if "m" ==0, then "mk" is invalid so no need to add this case
# if "m" != 0, then "mk" is valid so add the solution
# "../mk" from dp[i-2]
if i-2 >=-1 and int(nums[i-1]) !=0 and int(nums[i-1:i+1]) >0 and int(nums[i-1:i+1]) <=26:
if i-2 ==-1:
dp[i] += 1
else:
dp[i] += dp[i-2]
return dp[-1]