-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0730-count-different-palindromic-subsequences.js
59 lines (51 loc) · 1.79 KB
/
0730-count-different-palindromic-subsequences.js
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
/**
* 730. Count Different Palindromic Subsequences
* https://leetcode.com/problems/count-different-palindromic-subsequences/
* Difficulty: Hard
*
* Given a string s, return the number of different non-empty palindromic subsequences in s.
* Since the answer may be very large, return it modulo 109 + 7.
*
* A subsequence of a string is obtained by deleting zero or more characters from the string.
*
* A sequence is palindromic if it is equal to the sequence reversed.
*
* Two sequences a1, a2, ... and b1, b2, ... are different if there is some i for which ai != bi.
*/
/**
* @param {string} s
* @return {number}
*/
var countPalindromicSubsequences = function(s) {
const MOD = 1e9 + 7;
const dp = new Array(s.length).fill().map(() => {
return new Array(s.length).fill(0);
});
for (let i = 0; i < s.length; i++) {
dp[i][i] = 1;
}
for (let len = 2; len <= s.length; len++) {
for (let start = 0; start + len <= s.length; start++) {
const end = start + len - 1;
const charStart = s[start];
const charEnd = s[end];
if (charStart !== charEnd) {
dp[start][end] = (dp[start + 1][end] + dp[start][end - 1] - dp[start + 1][end - 1]) % MOD;
} else {
let left = start + 1;
let right = end - 1;
while (left <= right && s[left] !== charStart) left++;
while (left <= right && s[right] !== charStart) right--;
if (left > right) {
dp[start][end] = (2 * dp[start + 1][end - 1] + 2) % MOD;
} else if (left === right) {
dp[start][end] = (2 * dp[start + 1][end - 1] + 1) % MOD;
} else {
dp[start][end] = (2 * dp[start + 1][end - 1] - dp[left + 1][right - 1]) % MOD;
}
}
if (dp[start][end] < 0) dp[start][end] += MOD;
}
}
return dp[0][s.length - 1];
};