91. Decode Ways
Description
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)- The grouping
(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 <= 100scontains only digits and may contain leading zero(s).
Solutions
Solution 1: Dynamic Programming
Thinking
The first idea is recursion: at index \(i\), decode \(s[i]\) alone (if not \(0\)), or pair it with the previous digit into \(10\)–\(26\). \(n \le 100\), but without memoization the tree is exponential because the same prefix is decoded again and again.
The bottleneck is that the number of ways to decode the first \(i\) characters depends only on shorter prefixes, and a valid split is only “one digit / two digits”. That is climbing stairs with encoding constraints.
So let \(f[i]\) be the number of ways for the first \(i\) characters. The empty string has one way; \(0\) cannot stand alone; only \(10\)–\(26\) can be a pair. Scan once instead of enumerating every partition.
We define \(f[i]\) to represent the number of decoding methods for the first \(i\) characters of the string. Initially, \(f[0]=1\), and the rest \(f[i]=0\).
Consider how \(f[i]\) transitions.
- If the \(i\)th character (i.e., \(s[i-1]\)) forms a code on its own, it corresponds to one decoding method, i.e., \(f[i]=f[i-1]\). The premise is \(s[i-1] \neq 0\).
- If the string formed by the \(i-1\)th character and the \(i\)th character is within the range \([1,26]\), then they can be treated as a whole, corresponding to one decoding method, i.e., \(f[i] = f[i] + f[i-2]\). The premise is \(s[i-2] \neq 0\), and \(s[i-2]s[i-1]\) is within the range \([1,26]\).
The time complexity is \(O(n)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of the string.
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Solution 2: Optimized Dynamic Programming
Thinking
Solution 1 is already correct. Each \(f[i]\) only needs \(f[i-1]\) and \(f[i-2]\), so the full array is unnecessary. Two rolling variables cut space to \(O(1)\); time is still a single pass.
We notice that the state \(f[i]\) is only related to \(f[i-1]\) and \(f[i-2]\). Therefore, we can use two variables to replace these states, reducing the space complexity from \(O(n)\) to \(O(1)\). The time complexity remains \(O(n)\).
1 2 3 4 5 6 7 8 9 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |