📘
Decode Ways
MediumDynamic Programming
A message containing letters A-Z can be encoded as numbers: 'A' -> "1", 'B' -> "2", ..., 'Z' -> "26".
Given a string s containing only digits, return the number of ways to decode it.
Example:
Input:
226
Output:
3
Reasoning:
- The input string
226can be decoded as follows:2as 'B',2as 'B',6as 'F', resulting in one possible decoding: 'BBF'.
- Another possible decoding is:
22as 'V',6as 'F', resulting in 'VF'.
- A third possible decoding is:
2as 'B',26as 'Z', resulting in 'BZ'.
- The total number of ways to decode
226is the sum of these possibilities, which is 1+1+1=3. - The final output is 3.
Constraints:
- 1 <= len(s) <= 100
- s contains only digits
- s does not contain leading zeros except "0" itself
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.