-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathDecodeWaysMemo.java
61 lines (45 loc) · 1.55 KB
/
DecodeWaysMemo.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
package problems.leetcode;
import java.util.Arrays;
/**
* https://leetcode.com/problems/decode-ways/
*/
public class DecodeWaysMemo {
public int numDecodings(String s) {
int[] memo = new int[s.length()];
Arrays.fill(memo, -1);
return numDecodings(s, 0, memo);
}
private int numDecodings(String s, int i, int[] memo) {
/* If our decoding pointer out of bounds then we know that we have exhausted our ability to decode the string */
if (i >= s.length()) {
return 1;
} else if (memo[i] > -1) {
return memo[i];
}
int decodingCount = 0;
if (isValid(s, i, 1)) { // single character decoding
decodingCount += numDecodings(s, i + 1, memo);
}
if (isValid(s, i, 2)) { // 2 character decoding
/*
If this is a valid decoding then recurse on it since it is ONE valid way to decode
a piece of the string off. If it is INVALID we will not factor this way of decoding
in and the path in the "tree" of recursion is cut short
*/
decodingCount += numDecodings(s, i + 2, memo);
}
memo[i] = decodingCount;
return decodingCount;
}
private boolean isValid(String s, int i, int len) {
if (i + len > s.length()) {
return false;
}
s = s.substring(i, i + len);
if (s.charAt(0) == '0') {
return false;
}
int val = Integer.parseInt(s);
return val >= 1 && val <= 26;
}
}