Skip to content

Commit 4b7bc87

Browse files
committed
Leetcode | Decode Ways | Java
1 parent b7a8d17 commit 4b7bc87

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# 91. Decode Ways
2+
3+
You have intercepted a secret message encoded as a string of numbers. The message is decoded via the following mapping:
4+
5+
`"1" -> 'A'`
6+
7+
`"2" -> 'B'`
8+
9+
`...`
10+
11+
`"25" -> 'Y'`
12+
13+
`"26" -> 'Z'`
14+
15+
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"`).
16+
17+
For example, `"11106"` can be decoded into:
18+
19+
- `"AAJF"` with the grouping `(1, 1, 10, 6)`
20+
- `"KJF"` with the grouping `(11, 10, 6)`
21+
- The grouping `(1, 11, 06)` is invalid because `"06"` is not a valid code (only `"6"` is valid).
22+
23+
Note: there may be strings that are impossible to decode.
24+
25+
Given a string s containing only digits, return the number of ways to decode it. If the entire string cannot be decoded
26+
in any valid way, return 0.
27+
28+
The test cases are generated so that the answer fits in a 32-bit integer.
29+
30+
Example 1:
31+
32+
Input: s = "12"
33+
34+
Output: 2
35+
36+
Explanation:
37+
38+
"12" could be decoded as "AB" (1 2) or "L" (12).
39+
40+
Example 2:
41+
42+
Input: s = "226"
43+
44+
Output: 3
45+
46+
Explanation:
47+
48+
"226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
49+
50+
Example 3:
51+
52+
Input: s = "06"
53+
54+
Output: 0
55+
56+
Explanation:
57+
58+
"06" cannot be mapped to "F" because of the leading zero ("6" is different from "06"). In this case, the string is not a
59+
valid encoding, so return 0.
60+
61+
Constraints:
62+
63+
- `1 <= s.length <= 100`
64+
- `s` contains only digits and may contain leading zero(s).
65+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.devstromo.medium.p91;
2+
3+
import java.util.Arrays;
4+
5+
public class Solution {
6+
public int numDecodings(String s) {
7+
int n = s.length();
8+
int[] dp = new int[n];
9+
Arrays.fill(dp, -1);
10+
return dfs(0, s, dp);
11+
}
12+
13+
private int dfs(int index, String s, int[] dp) {
14+
if (index == s.length()) return 1; // Reached end: 1 valid decoding path
15+
if (s.charAt(index) == '0') return 0; // '0' can't start a valid code
16+
if (dp[index] != -1) return dp[index];
17+
18+
int result = dfs(index + 1, s, dp);
19+
20+
if (index + 1 < s.length()) {
21+
int num = Integer.parseInt(s.substring(index, index + 2));
22+
if (num >= 10 && num <= 26) {
23+
result += dfs(index + 2, s, dp);
24+
}
25+
}
26+
27+
dp[index] = result;
28+
return result;
29+
}
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.devstromo.medium.p91;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class SolutionUnitTest {
9+
private final Solution solution = new Solution();
10+
11+
@Test
12+
@DisplayName("Test Decode Ways")
13+
void testDecodeWays() {
14+
assertEquals(2, solution.numDecodings("12"));
15+
assertEquals(3, solution.numDecodings("226"));
16+
assertEquals(0, solution.numDecodings("06"));
17+
}
18+
19+
}

0 commit comments

Comments
 (0)