-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay96.java
More file actions
45 lines (40 loc) · 1.54 KB
/
Day96.java
File metadata and controls
45 lines (40 loc) · 1.54 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
33
34
35
36
37
38
39
40
41
42
43
44
45
public class Day96 {
public static int countPalindromicSubsequences(String s) {
int mod = 1000000007;
int n = s.length();
int[][] dp = new int[n][n];
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
if (i == j) {
dp[i][j] = 1; // Single character is a palindrome
} else {
if (s.charAt(i) == s.charAt(j)) {
int l = i + 1;
int r = j - 1;
while (l <= r && s.charAt(i) != s.charAt(l)) {
l++;
}
while (l <= r && s.charAt(j) != s.charAt(r)) {
r--;
}
if (l > r) {
dp[i][j] = dp[i + 1][j - 1] * 2 + 2;
} else if (l == r) {
dp[i][j] = dp[i + 1][j - 1] * 2 + 1;
} else {
dp[i][j] = dp[i + 1][j - 1] * 2 - dp[l + 1][r - 1];
}
} else {
dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1];
}
}
dp[i][j] = (dp[i][j] < 0) ? dp[i][j] + mod : dp[i][j] % mod;
}
}
return dp[0][n - 1];
}
public static void main(String[] args) {
String s = "pqqr";
System.out.println("Output: " + countPalindromicSubsequences(s));
}
}