DP with two variables. At each position, check if -digit and -digit decodings are valid.
function numDecodings(s):
if s[0] == '0':
return 0
n = len(s)
prev2 = 1
prev1 = 1
for i from 1 to n-1:
curr = 0
if s[i] != '0':
curr += prev1
twoDigit = int(s[i-1:i+1])
if twoDigit >= 10 and twoDigit <= 26:
curr += prev2
prev2 = prev1
prev1 = curr
return prev1
time, space.