Given a non-empty string containing only digits, write a function to count the number of ways to decode it. A valid decoding follows the mapping: 'A' -> 1
, 'B' -> 2
, ..., 'Z' -> 26
. For example, the string '12'
can be decoded as 'AB'
(1 2) or 'L'
(12).
Your task is to implement the function with a dynamic programming approach to efficiently count all possible decodings.
s
consisting of digits from '0' to '9'.Input: s = "226"
Output: 3
Explanation:
On this day in history, many great innovations remind us that creativity and persistence are key to solving challenging problems. Use a dynamic programming approach to optimize your solution.
Happy coding!