g0001_0100.s0091_decode_ways.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0001_0100.s0091_decode_ways;
// #Medium #Top_Interview_Questions #String #Dynamic_Programming #Dynamic_Programming_I_Day_10
// #2022_02_21_Time_2_ms_(64.33%)_Space_42.4_MB_(18.84%)
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];
}
}