forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
32 lines (30 loc) · 1.05 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package g0001_0100.s0091_decode_ways;
// #Medium #Top_Interview_Questions #String #Dynamic_Programming
// #Algorithm_II_Day_15_Dynamic_Programming #Dynamic_Programming_I_Day_10
// #2022_06_21_Time_2_ms_(66.37%)_Space_41.8_MB_(78.45%)
public class Solution {
public int numDecodings(String s) {
if (s.charAt(0) == '0') {
return 0;
}
int n = s.length();
int[] f = new int[n + 1];
// Auxiliary
f[0] = 1;
f[1] = 1;
for (int i = 2; i <= n; i++) {
// Calculate the independent number
if (s.charAt(i - 1) != '0') {
// As long as the current character is not 0, it means that the previous decoding
// number can be inherited
f[i] = f[i - 1];
}
// Calculate the number of combinations
int twodigits = (s.charAt(i - 2) - '0') * 10 + (s.charAt(i - 1) - '0');
if (twodigits >= 10 && twodigits <= 26) {
f[i] += f[i - 2];
}
}
return f[n];
}
}