Decode Ways

 

Problem Statement: A message containing letters from A-Z can be encoded into numbers using the following mapping:

rust
'A' -> "1" 'B' -> "2" ... 'Z' -> "26"

To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, "11106" can be mapped into:

  • "AAJF" with the grouping (1 1 10 6)
  • "KJF" with the grouping (11 10 6) Note that the grouping (1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is a single-digit number and must be treated separately.

Given a non-empty string num containing only digits, return the number of ways to decode it.

1. Clarify the problem:

  • Can the input string contain leading zeros?
  • Can the input string be empty?
  • Can the input string contain characters other than digits?

2. Analyze the problem:

  • Input: Non-empty string num containing only digits
  • Output: Number of ways to decode the string
  • Constraints:
    • The input string can contain leading zeros.
    • The input string will only contain digits.

3. Design an algorithm:

  • Initialize two variables, prev and curr, both set to 1.
  • Iterate through the string starting from the second character:
    • If the current character is '0':
      • If the previous character is '1' or '2', update curr to be equal to prev.
      • Otherwise, return 0 since a single '0' cannot be decoded.
    • If the previous character is '1' or the previous character is '2' and the current character is between '1' and '6' (inclusive), update curr to be equal to the sum of curr and prev.
    • Update prev to be equal to its previous value.
  • Return the value of curr.

4. Explain your approach:

  • We will iterate through the string and update the values of prev and curr accordingly. The variable prev represents the number of ways to decode the string up to the previous character, while curr represents the number of ways to decode the string up to the current character.
  • If the current character is '0', we need to handle some special cases. If the previous character is '1' or '2', we can treat the '0' as a separate character and update curr to be equal to prev. Otherwise, a single '0' cannot be decoded, so we return 0.
  • If the previous character is '1' or '2' and the current character is between '1' and '6' (inclusive), we can treat the two characters as a single unit and update curr to be the sum of curr and prev.
  • After iterating through the string, we return the value of curr, which represents the number of ways to decode the entire string.

5. Write clean and readable code:

python
class Solution: def numDecodings(self, num: str) -> int: if num[0] == '0': return 0 prev = curr = 1 for i in range(1, len(num)): if num[i] == '0': if num[i - 1] == '1' or num[i - 1] == '2': curr = prev else: return 0 elif num[i - 1] == '1' or (num[i - 1] == '2' and '1' <= num[i] <= '6'): prev, curr = curr, curr + prev else: prev = curr return curr

6. Test your code: I will test the code with the following test cases:

  • Test case 1: num = "12"
  • Test case 2: num = "226"
  • Test case 3: num = "0"
  • Test case 4: num = "06"
  • Test case 5: num = "10"
python
solution = Solution() print(solution.numDecodings("12")) # Expected output: 2 print(solution.numDecodings("226")) # Expected output: 3 print(solution.numDecodings("0")) # Expected output: 0 print(solution.numDecodings("06")) # Expected output: 0 print(solution.numDecodings("10")) # Expected output: 1

7. Optimize if necessary: The given solution is already efficient with a time complexity of O(n), where n is the length of the input string.

8. Handle error cases:

  • If the input string is empty, we can return 0.
  • If the input string contains characters other than digits, we can return 0.

9. Discuss complexity analysis:

  • The time complexity of the solution is O(n), where n is the length of the input string. This is because we iterate through the string once.
  • The space complexity is O(1) since we use a constant amount of space to store the variables prev and curr.
Next Post Previous Post