You have intercepted a secret message encoded as a string of numbers. The message is decoded via the following mapping:
"1" -> 'A'
"2" -> 'B'
...
"25" -> 'Y'
"26" -> 'Z'
However, while decoding the message, you realize that there are many different ways you can decode the message because some codes are contained in other codes ("2" and "5" vs "25").
For example, "11106" can be decoded into:
"AAJF" with the grouping (1, 1, 10, 6)"KJF" with the grouping (11, 10, 6)(1, 11, 06) is invalid because "06" is not a valid code (only "6" is valid).Note: there may be strings that are impossible to decode.
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.
The test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Input: s = "12"
Output: 2
Explanation:
"12" could be decoded as "AB" (1 2) or "L" (12).
Example 2:
Input: s = "226"
Output: 3
Explanation:
"226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
Example 3:
Input: s = "06"
Output: 0
Explanation:
"06" cannot be mapped to "F" because of the leading zero ("6" is different from "06"). In this case, the string is not a valid encoding, so return 0.
Constraints:
1 <= s.length <= 100s contains only digits and may contain leading zero(s).When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a secret message made of numbers where each letter of the alphabet is represented by a number from one to twenty-six, and your job is to find every single possible way to spell out words using that number string. The brute force approach works by trying out every possible path of translating the numbers into letters from start to finish.
Here's how the algorithm would work step-by-step:
Instead of exploring every possible secret message translation at once, the optimal approach works backward from the end of the message to calculate the total number of valid ways piece by piece. By remembering the results of previous translations as we move along, we avoid repeating work and instantly figure out how many combinations are possible.
Here's how the algorithm would work step-by-step:
def num_decodings(secret_message): message_length = len(secret_message) ways_from_end = [0] * (message_length + 1) ways_from_end[message_length] = 1 # Working backward avoids redundant calculations by storing previous translation results. for current_index in range(message_length - 1, -1, -1): if secret_message[current_index] == '0': ways_from_end[current_index] = 0 else: ways_from_end[current_index] = ways_from_end[current_index + 1] # Combining two characters is valid only when the resulting number falls between 10 and 26. if current_index + 1 < message_length: two_digit_prefix = secret_message[current_index : current_index + 2] if '10' <= two_digit_prefix <= '26': ways_from_end[current_index] += ways_from_end[current_index + 2] # Reaching the beginning accumulates all valid paths into the final total combination count. return ways_from_end[0]| Case | How to Handle |
|---|---|
| String starting with the character zero | The dp state for this index must be zero because no valid letter maps to a standalone zero. |
| String containing consecutive zeros like double zero | Invalid two-digit combinations will yield zero ways, halting the propagation of valid decodings. |
| Single-digit valid string such as a string containing only five | Base cases correctly initialize the dynamic programming array to return one valid decoding way. |
| Two-digit number greater than twenty-six such as fifty-six | The algorithm checks the numerical range and only adds the two-digit decode path if it falls between ten and twenty-six. |
| Two-digit number ending in zero such as ten or twenty | The algorithm must correctly recognize these as valid single two-digit decodings while preventing invalid single-digit parses of the zero. |
| Long strings with maximum constraints | Using an iterative dynamic programming approach with constant space ensures optimal performance and prevents stack overflow. |
| Inputs with no valid decoding path whatsoever | The DP state naturally accumulates a final result of zero, correctly indicating that no valid decoding exists. |