-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0091-decode-ways.java
70 lines (64 loc) · 1.84 KB
/
0091-decode-ways.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Optimal
class Solution {
public int numDecodings(String s) {
int twoBack = 1; // empty string
int oneBack = s.charAt(0) == '0' ? 0 : 1;
int current = oneBack;
for (int i = 2; i < s.length() + 1; i++) {
current = 0;
if (s.charAt(i - 1) != '0') {
current += oneBack;
}
if (
s.charAt(i - 2) == '1' ||
(s.charAt(i - 2) == '2' && s.charAt(i - 1) < '7')
) {
current += twoBack;
}
twoBack = oneBack;
oneBack = current;
}
return current;
}
}
//bottom up
class Solution {
public int numDecodings(String s) {
int[] dp = new int[s.length() + 1];
dp[0] = 1; // empty string
dp[1] = s.charAt(0) == '0' ? 0 : 1;
for (int i = 2; i < s.length() + 1; i++) {
if (s.charAt(i - 1) != '0') {
dp[i] += dp[i - 1];
}
if (
s.charAt(i - 2) == '1' ||
(s.charAt(i - 2) == '2' && s.charAt(i - 1) < '7')
) {
dp[i] += dp[i - 2];
}
}
return dp[s.length()];
}
}
//top down with memoization
class Solution {
public int numDecodings(String s) {
return numDecodings(s, 0, new Integer[s.length()]);
}
private int numDecodings(String s, int i, Integer[] dp) {
if (i == s.length()) return 1;
if (s.charAt(i) == '0') return 0;
if (dp[i] != null) return dp[i];
int count = 0;
count += numDecodings(s, i + 1, dp);
if (
i < s.length() - 1 &&
(s.charAt(i) == '1' || s.charAt(i) == '2' && s.charAt(i + 1) < '7')
) {
count += numDecodings(s, i + 2, dp);
}
dp[i] = count;
return dp[i];
}
}