## Similar Problems

Similar Problems not available

# Decode Ways - Leetcode Solution

LeetCode: Decode Ways Leetcode Solution

Difficulty: Medium

Topics: string dynamic-programming

Problem:

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 different from "06".

Given a string s containing only digits, return the number of ways to decode it.

The answer is guaranteed to fit in a 32-bit integer.

Example 1:

Input: s = "12" Output: 2 Explanation: "12" can be decoded as "AB" (1 2) or "L" (12).

Example 2:

Input: s = "226" Output: 3 Explanation: "226" can be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).

Example 3:

Input: s = "0" Output: 0 Explanation: There is no character that maps to a number starting with 0. The only valid mappings with 0 are 'J' -> "10" and 'T' -> "20", neither of which start with 0. Hence, there are no valid ways to decode this since all digits need to be mapped.

Example 4:

Input: s = "06" Output: 0 Explanation: "06" cannot be mapped to "F" because of the leading zero ("6" is different from "06").

Constraints:

1 <= s.length <= 100 s contains only digits and may contain leading zero(s).

Solution:

Approach: We can solve this problem using dynamic programming. We can define dp[i] as the number of ways to decode the substring from 0 to i in the given string s. We can utilize the previously calculated dp values to calculate the current dp value. We can check two cases while calculating the current dp value:

Case 1: If the current digit is not zero, we can include it as a single digit and add the number of ways to decode the substring from (i-1) to the current index.

Case 2: If the current digit, along with the previous digit, forms a valid two-digit number, we can include it as a two-digit number and add the number of ways to decode the substring from (i-2) to the current index.

We can return dp[n-1], where n is the length of the given string s, as the desired answer.

Algorithm:

- Initialize dp[0] as 1, since there is one way to decode a string of length 1.
- If the first digit is zero, return 0, since the first digit cannot be mapped to any letter.
- Loop from i=1 to n-1, where n is the length of the given string s.
- If s[i] is not zero, add dp[i-1] to dp[i], since we can include s[i] as a single digit.
- If s[i-1] and s[i] form a valid two-digit number, add dp[i-2] to dp[i], since we can include s[i-1] and s[i] as a two-digit number.
- Return dp[n-1], where n is the length of the given string s, as the desired answer.

Time Complexity: O(n), where n is the length of the given string s. Space Complexity: O(n), where n is the length of the given string s.

Code:

class Solution { public: int numDecodings(string s) { int n = s.size(); vector<int> dp(n, 0); dp[0] = 1; if(s[0] == '0') return 0; for(int i = 1; i < n; i++) { if(s[i] != '0') dp[i] += dp[i-1]; int two_digit_number = (s[i-1] - '0')*10 + (s[i] - '0'); if(two_digit_number >= 10 && two_digit_number <= 26) { if(i >= 2) dp[i] += dp[i-2]; else dp[i]++; } } return dp[n-1]; } };

## Decode Ways Solution Code

`1`