-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDecodeWays.java
More file actions
28 lines (25 loc) · 838 Bytes
/
DecodeWays.java
File metadata and controls
28 lines (25 loc) · 838 Bytes
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
public class Solution {
public int numDecodings(String s) {
if (s.length() == 0) {
return 0;
}
int f0 = validCode(s.substring(0, 1)) ? 1 : 0;
if (s.length() == 1) {
return f0;
}
int f1 = (validCode(s.substring(1, 2)) ? f0 : 0) + (validCode(s.substring(0, 2)) ? 1 : 0);
for (int i = 2; i < s.length(); ++i) {
int f2 = (validCode(s.substring(i, i + 1)) ? f1 : 0) + (validCode(s.substring(i - 1, i + 1)) ? f0 : 0);
f0 = f1;
f1 = f2;
}
return f1;
}
public boolean validCode(String str) {
if (str.charAt(0) == '0') {
return false;
}
int val = Integer.parseInt(str);
return val >= 1 && val <= 26;
}
}