-
Notifications
You must be signed in to change notification settings - Fork 0
/
0091_Decode_Ways.java
50 lines (41 loc) · 2.03 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
// recursion
class Solution {
private int numDecodings(String s, int i) {
if(i == s.length()) return 1; // base-case
if(s.charAt(i) == '0') return 0; // leading zero- no possible decodings
int result = 0;
for(int l = 1; l < 27; l++) { // for each letter
if(l < 10) { // one-digit-code letters
if((int) s.charAt(i) == l + '0') result += numDecodings(s, i + 1); // recursively find # matches
} else { // two-digit-code letters
if(i == s.length() - 1) break; // only one digit to match
if((int) s.charAt(i) == (l / 10) + '0' && (int) s.charAt(i + 1) == (l % 10) + '0')
result += numDecodings(s, i + 2);
}
}
return result;
}
public int numDecodings(String s) {
return numDecodings(s, 0); // start at index 0
}
}
// iteration
class Solution {
public int numDecodings(String s) {
int[] decodingCounts = new int[s.length() + 1]; // dC[i] holds the # of decodings of substring s[i:end]
decodingCounts[s.length()] = 1; // any decoding that reaches the end of s counts as a single decoding
for(int i = s.length() - 1; i >= 0; i--) { // iterate through s backwards
if(s.charAt(i) == '0') continue; // 0 cannot be decoded.
// s[i] is a valid 1-digit-code - there are dC[i + 1] decodings if s[i] is
// interpreted as a 1-digit-code
decodingCounts[i] += decodingCounts[i + 1];
// check if s[i -> i + 1] is a valid 2-digit-code
if(i == s.length() - 1) continue; // cannot be a 2-digit-code
if(s.charAt(i) == '1' || (s.charAt(i) == '2' && s.charAt(i + 1) < '7')) {
decodingCounts[i] += decodingCounts[i + 2]; // there are dC[i + 2] decodings if s[i] is
// interpreted as a 2-digit-code
}
}
return decodingCounts[0];
}
}