A message containing letters from A-Z can be encoded into numbers using the mapping: 'A' -> 1, 'B' -> 2, ..., 'Z' -> 26. Given a string `s` containing only digits, return the number of ways to decode it. If the entire string cannot be decoded in any valid way, return 0.
Linear Dynamic Programming
At any index `i`, the number of ways to decode the substring `s[i:]` depends on: (1) whether `s[i]` is a valid single-digit code ('1'-'9') and (2) whether `s[i...i+1]` is a valid two-digit code ('10'-'26'). This allows us to build the solution using the recurrence `dp[i] = dp[i+1] + dp[i+2]` (where applicable).