forked from luckykumardev/leetcode-solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecode Ways.java
90 lines (73 loc) · 1.79 KB
/
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class Solution {
public int numDecodings(String s) {
//recursive
// int ans = numDecodings(s, 0);
//memo
// int arr[] = new int[s.length() +1];
// int ans = numDecodings(s, 0, arr);
//tabulation
int ans = numDecodingsTab(s);
return ans;
}
//tabulation
public int numDecodingsTab(String s) {
if(s == null || s.length() == 0) {
return 0;
}
int n = s.length();
int[] dp = new int[n+1];
dp[0] = 1;
dp[1] = s.charAt(0) != '0' ? 1 : 0;
for(int i = 2; i <= n; i++) {
int first = Integer.valueOf(s.substring(i-1, i));
int second = Integer.valueOf(s.substring(i-2, i));
if(first >= 1 && first <= 9) {
dp[i] += dp[i-1];
}
if(second >= 10 && second <= 26) {
dp[i] += dp[i-2];
}
}
return dp[n];
}
//memo
private int numDecodings(String s, int index, int arr[]) {
if (index == s.length() ) {
return 1;
}
if (s.charAt(index) == '0') {
return 0;
}
if (index == s.length() - 1) {
return 1;
}
if(arr[index] > 0){
return arr[index];
}
int way1 = numDecodings(s, index + 1, arr);
int way2 = 0;
if (Integer.parseInt(s.substring(index, index + 2)) <= 26) {
way2 = numDecodings(s, index + 2, arr);
}
arr[index] = way1 + way2;
return arr[index];
}
//recusive
private int numDecodings(String s, int index) {
if (index == s.length() ) {
return 1;
}
if (s.charAt(index) == '0') {
return 0;
}
if (index == s.length() - 1) {
return 1;
}
int way1 = numDecodings(s, index + 1);
int way2 = 0;
if (Integer.parseInt(s.substring(index, index + 2)) <= 26) {
way2 = numDecodings(s, index + 2);
}
return way1 + way2;
}
}