Advertisement

Decode Ways II - LeetCode 639 Solution

Decode Ways II - Complete Solution Guide

Decode Ways II is LeetCode problem 639, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

A message containing letters from A-Z can be encoded into numbers using the following mapping: '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

Detailed Explanation

The problem asks us to find the number of ways to decode a string consisting of digits and '*' characters, where each digit maps to a letter A-Z (1->A, 2->B, ..., 26->Z). The '*' character can represent any digit from 1 to 9. We need to return the number of possible decodings modulo 10^9 + 7.

Solution Approach

The solution uses dynamic programming with constant space optimization. We maintain two variables, `e0` and `e1`, representing the number of ways to decode the string up to the (i-1)th and ith positions respectively. For each position i, we calculate the number of ways to decode the string up to that position (`e2`) based on the characters at positions (i-1) and i. Then, we update `e0` and `e1` for the next iteration.

Step-by-Step Algorithm

  1. Step 1: Initialize `e0` to 1, which represents the base case of an empty string having one way to decode.
  2. Step 2: Handle the first character of the string. If it is '0', there are no ways to decode the string, so return 0. If it is '*', `e1` is 9 (since '*' can represent 1-9), otherwise `e1` is 1.
  3. Step 3: Iterate through the string starting from the second character (index 1).
  4. Step 4: For each character `c2` at index i, calculate `e2` based on `c2` and the previous character `c1` at index i-1.
  5. Step 5: Calculate the number of ways to decode `c2` as a single character. If `c2` is '*', multiply the number of ways by 9 (as it represents digits 1-9). If `c2` is a digit greater than '0', the number of ways is the same as the previous single character case. Otherwise, it's zero.
  6. Step 6: Calculate the number of ways to decode `c1` and `c2` together as a two-digit number. This requires handling several cases: - If `c1` is '1', and `c2` is '*', then we have 9 additional ways to decode (11, 12, ..., 19). - If `c1` is '1', and `c2` is a digit, we have one additional way to decode (if it valid). - If `c1` is '2', and `c2` is '*', then we have 6 additional ways to decode (21, 22, ..., 26). - If `c1` is '2', and `c2` is a digit between '0' and '6', we have one additional way to decode. - If `c1` is '*', and `c2` is '*', then we have 15 additional ways to decode (11-19, 21-26). This is because '*' can be '1' or '2' for the first digit and 1-9 or 1-6 respectively, for the second digit. - If `c1` is '*', and `c2` is a digit between '0' and '6', we have 2 additional ways to decode (1x, 2x). - If `c1` is '*', and `c2` is a digit greater than '6', we have 1 additional way to decode (1x).
  7. Step 7: Update `e0` to `e1` and `e1` to `e2` for the next iteration.
  8. Step 8: After iterating through the string, return `e1` as the final number of ways to decode the string, modulo 10^9 + 7.

Key Insights

  • Insight 1: Dynamic programming is the best approach because the number of ways to decode a string depends on the number of ways to decode its prefixes. This allows us to build up the solution iteratively.
  • Insight 2: We need to handle the '*' character carefully, as it can represent multiple digits. We must correctly calculate the number of possibilities that the '*' contributes in both single-digit and two-digit decoding.
  • Insight 3: The problem involves considering pairs of characters and single characters, requiring accurate case analysis based on the possible values of the '*' character and its position.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: String, Dynamic Programming.

Companies

Asked at: PhonePe.