Decode Ways - Complete Solution Guide
Decode Ways is LeetCode problem 91, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 i
Detailed Explanation
The "Decode Ways" problem presents a scenario where a string of digits needs to be decoded into a sequence of letters, based on a given mapping (1 -> 'A', 2 -> 'B', ..., 26 -> 'Z'). The goal is to determine the number of possible ways to decode the input string. A single digit can represent one letter, or two digits can represent one letter (if the combined number is between 10 and 26, inclusive). The problem statement also specifies some key constraints: the string only contains digits, and leading zeros are invalid when interpreting two-digit combinations (e.g., '06' is invalid). The output should be the total number of valid decoding possibilities.
Solution Approach
The provided solutions use a dynamic programming approach with space optimization. Instead of storing the number of ways to decode each prefix of the string in an array, the solutions maintain only two variables: `one_back` and `two_back`. `one_back` stores the number of ways to decode the substring up to the previous character, and `two_back` stores the number of ways to decode the substring up to two characters before the current one. The algorithm iterates through the string, updating these variables based on whether the current character can be decoded as a single-digit number (1-9) or whether the combination of the previous and current characters can be decoded as a two-digit number (10-26). The final value of `one_back` represents the total number of ways to decode the entire string.
Step-by-Step Algorithm
- Step 1: Handle the base cases. If the first character is '0', there are no valid decodings, so return 0. Initialize `one_back` to 1, representing one way to decode an empty string or a single-digit string from 1 to 9. Initialize `two_back` to 1, also representing the base case of an empty string.
- Step 2: Iterate through the string from the second character (index 1) to the end.
- Step 3: In each iteration, initialize `current` to 0. `current` will store the number of ways to decode the string up to the current character.
- Step 4: Check if the current character is not '0'. If it's not '0', it can be decoded as a single-digit number (1-9), so add `one_back` to `current`. This means we can extend all previous decodings by adding the current digit as a separate letter.
- Step 5: Check if the previous character is '1' or if the previous character is '2' and the current character is less than or equal to '6'. If this condition is true, the previous and current characters can be combined to form a two-digit number between 10 and 26, inclusive. In this case, add `two_back` to `current`. This means we can extend all decodings from two characters ago by decoding the last two characters together.
- Step 6: Update `two_back` to `one_back` and `one_back` to `current`. This prepares for the next iteration by shifting the previous decoding counts.
- Step 7: After the loop finishes, return `one_back`, which now holds the total number of ways to decode the entire string.
Key Insights
- Insight 1: Dynamic Programming is suitable because the number of ways to decode a string depends on the number of ways to decode its prefixes.
- Insight 2: The current number of ways to decode can be determined using only the previous one or two values, suggesting a space-optimized DP solution.
- Insight 3: Handling the '0' case is crucial, as a single '0' is not a valid decoding, and a '0' following a '1' or '2' affects the decoding count.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: String, Dynamic Programming.
Companies
Asked at: Adobe, Amazon, Apple, Bloomberg, Commvault, Flipkart, Goldman Sachs, Graviton, J.P. Morgan, Lyft, Meta, Microsoft, Moengage, Morgan Stanley, Oracle, Salesforce, Snap, TikTok, Uber, Walmart Labs, Zoho.