-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDP14CountEncoding.java
More file actions
32 lines (29 loc) · 1.05 KB
/
Copy pathDP14CountEncoding.java
File metadata and controls
32 lines (29 loc) · 1.05 KB
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
import java.util.*;
public class DP14CountEncoding {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
int[] dp = new int[str.length()];
dp[0] = 1;
for (int i = 1; i < dp.length; i++) {
if (str.charAt(i - 1) == '0' && str.charAt(i) == '0') {
dp[i] = 0;
} else if (str.charAt(i - 1) == '0') {
dp[i] = dp[i - 1];
} else if (str.charAt(i) == '0') {
if (str.charAt(i - 1) == '1' || str.charAt(i - 1) == '2') {
dp[i] = (i >= 2 ? dp[i - 2] : 1);
} else {
dp[i] = 0;
}
} else {
if (Integer.parseInt(str.substring(i - 1, i + 1)) <= 26) {
dp[i] = dp[i - 1] + (i >= 2 ? dp[i - 2] : 1);
} else {
dp[i] = dp[i - 1];
}
}
}
System.out.println(dp[str.length() - 1]);
}
}