91. Decode Ways
[Medium] “A message containing letters from A-Z is being encoded to integers 1-26."
Python3:
class Solution:
def numDecodings(self, s: str) -> int:
# says non-empty but good to check :)
if not s:
return 0
res = [0] * (len(s) + 1)
res[0] = 1
res[1] = 0 if s[0] == "0" else 1
for i in range(2, len(s) + 1):
# decode one
if int(s[i-1:i]) > 0:
res[i] += res[i - 1]
# decode two
if 10 <= int(s[i-2:i]) <= 26:
res[i] += res[i - 2]
return res[-1]