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
.
Example 1:
s = "12"
Output: 2
Explanation: "12" could be decoded as "AB" (1 2) or "L" (12).
Example 2:
s = "226"
Output: 3
Explanation: "226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
Example 3:
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.
What are the time and space complexities of your solution?
Given a string s
containing only digits, determine the number of ways to decode it. The mapping is as follows:
'A' -> "1"
'B' -> "2"
...
'Z' -> "26"
For instance, "12" can be decoded as "AB" (1, 2) or "L" (12). The string "226" can be decoded as "BZ" (2, 26), "VF" (22, 6), or "BBF" (2, 2, 6).
A straightforward approach involves recursion. We can explore all possible groupings of digits (single or double digits) and check if they represent valid codes (1-26). If a grouping is valid, we proceed to decode the remaining part of the string.
def num_decodings_recursive(s):
if not s:
return 1
if s[0] == '0':
return 0
count = 0
# Single digit
count += num_decodings_recursive(s[1:])
# Two digits
if len(s) >= 2 and 10 <= int(s[:2]) <= 26:
count += num_decodings_recursive(s[2:])
return count
The time complexity is exponential, approximately O(2^n), where n is the length of the string, as we explore each possible branch of the recursion tree.
The space complexity is O(n) due to the depth of the recursion stack.
A more efficient solution can be achieved using dynamic programming. We create a dp
array where dp[i]
represents the number of ways to decode the substring s[:i]
. The base cases are dp[0] = 1
(empty string has one way to decode) and dp[1] = 1
if s[0] != '0'
. Otherwise, dp[1] = 0
.
def num_decodings_dp(s):
n = len(s)
dp = [0] * (n + 1)
dp[0] = 1
dp[1] = 0 if s[0] == '0' else 1
for i in range(2, n + 1):
# Single digit
if '1' <= s[i-1] <= '9':
dp[i] += dp[i-1]
# Two digits
two_digit = int(s[i-2:i])
if 10 <= two_digit <= 26:
dp[i] += dp[i-2]
return dp[n]
The time complexity is O(n), where n is the length of the string, as we iterate through the string once to populate the dp
array.
The space complexity is O(n) to store the dp
array. This can be further optimized to O(1) by storing only the previous two values, but for clarity, we'll stick to the O(n) space complexity.
dp[1]
considers cases where the first digit is zero.