Count digit decodings
Count ways to decode a digit string with 1=A through 26=Z. Leading zero groups are invalid. Empty text returns zero.
text = '26'
print(text[-1] != '0')
print(10 <= int(text) <= 26)Reason about the state
A prefix ends in either one valid nonzero digit or a valid two-digit code. Keep the previous two prefix counts for linear time and constant space. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Count ways to decode a digit string with 1=A through 26=Z. Leading zero groups are invalid. Empty text returns zero.
The explanation opens here once every check passes.