Decode Ways
Solution Code
class Solution:
def numDecodings(self, s: str) -> int:
if not s or s[0] == '0':
return 0
n = len(s)
two_back = 1
one_back = 1
for i in range(2, n + 1):
current = 0
if s[i - 1] != '0':
current += one_back
two_digit = int(s[i - 2:i])
if 10 <= two_digit <= 26:
current += two_back
two_back = one_back
one_back = current
return one_backTime Complexity
O(N)
Space Complexity
O(1)

