-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2472-maximum-number-of-non-overlapping-palindrome-substrings.js
More file actions
51 lines (47 loc) · 1.41 KB
/
2472-maximum-number-of-non-overlapping-palindrome-substrings.js
File metadata and controls
51 lines (47 loc) · 1.41 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
46
47
48
49
50
51
/**
* 2472. Maximum Number of Non-overlapping Palindrome Substrings
* https://leetcode.com/problems/maximum-number-of-non-overlapping-palindrome-substrings/
* Difficulty: Hard
*
* You are given a string s and a positive integer k.
*
* Select a set of non-overlapping substrings from the string s that satisfy the following
* conditions:
* - The length of each substring is at least k.
* - Each substring is a palindrome.
*
* Return the maximum number of substrings in an optimal selection.
*
* A substring is a contiguous sequence of characters within a string.
*/
/**
* @param {string} s
* @param {number} k
* @return {number}
*/
var maxPalindromes = function(s, k) {
const n = s.length;
const isPal = new Array(n).fill().map(() => new Array(n).fill(false));
const dp = new Array(n + 1).fill(0);
for (let len = 1; len <= n; len++) {
for (let start = 0; start + len <= n; start++) {
const end = start + len - 1;
if (len === 1) {
isPal[start][end] = true;
} else if (len === 2) {
isPal[start][end] = s[start] === s[end];
} else {
isPal[start][end] = s[start] === s[end] && isPal[start + 1][end - 1];
}
}
}
for (let end = k; end <= n; end++) {
dp[end] = dp[end - 1];
for (let start = end - k; start >= 0; start--) {
if (isPal[start][end - 1]) {
dp[end] = Math.max(dp[end], dp[start] + 1);
}
}
}
return dp[n];
};