Input: s ="abaccdbbd", k =3Output: 2Explanation: We can select the substrings underlined in s ="_**aba**_ cc _**dbbd**_ ". Both "aba" and "dbbd" are palindromes and have a length of at least k =3.It can be shown that we cannot find a selection with more than two valid substrings.
We want to select the maximum number of non-overlapping palindromic substrings of length at least k. We can use dynamic programming to precompute all palindromic substrings, then greedily select the earliest non-overlapping palindromes.
Precompute a 2D boolean array is_pal[i][j] indicating if s[i:j+1] is a palindrome.
For each possible substring of length at least k, mark it as a palindrome if it is.
Use a greedy approach: start from the beginning, and whenever a palindrome of length at least k is found, select it and jump to the end of that substring.
classSolution {
public:int maxPalindromes(string s, int k) {
int n = s.size(), ans =0, i =0;
vector<vector<bool>> is_pal(n, vector<bool>(n, false));
for (int len =1; len <= n; ++len) {
for (int l =0; l + len -1< n; ++l) {
int r = l + len -1;
if (s[l] == s[r] && (len <=2|| is_pal[l+1][r-1]))
is_pal[l][r] = true;
}
}
while (i <= n - k) {
bool found = false;
for (int j = i + k -1; j < n; ++j) {
if (is_pal[i][j]) {
ans++;
i = j +1;
found = true;
break;
}
}
if (!found) i++;
}
return ans;
}
};
classSolution {
publicintmaxPalindromes(String s, int k) {
int n = s.length(), ans = 0, i = 0;
boolean[][] isPal =newboolean[n][n];
for (int len = 1; len <= n; len++) {
for (int l = 0; l + len - 1 < n; l++) {
int r = l + len - 1;
if (s.charAt(l) == s.charAt(r) && (len <= 2 || isPal[l+1][r-1]))
isPal[l][r]=true;
}
}
while (i <= n - k) {
boolean found =false;
for (int j = i + k - 1; j < n; j++) {
if (isPal[i][j]) {
ans++;
i = j + 1;
found =true;
break;
}
}
if (!found) i++;
}
return ans;
}
}
classSolution {
funmaxPalindromes(s: String, k: Int): Int {
val n = s.length
val isPal = Array(n) { BooleanArray(n) }
for (len in1..n) {
for (l in0..n-len) {
val r = l + len - 1if (s[l] == s[r] && (len <=2|| isPal[l+1][r-1]))
isPal[l][r] = true }
}
var ans = 0var i = 0while (i <= n - k) {
var found = falsefor (j in i + k - 1 until n) {
if (isPal[i][j]) {
ans++ i = j + 1 found = truebreak }
}
if (!found) i++ }
return ans
}
}
classSolution:
defmaxPalindromes(self, s: str, k: int) -> int:
n = len(s)
is_pal = [[False]*n for _ in range(n)]
for length in range(1, n+1):
for l in range(n-length+1):
r = l + length -1if s[l] == s[r] and (length <=2or is_pal[l+1][r-1]):
is_pal[l][r] =True ans, i =0, 0while i <= n - k:
found =Falsefor j in range(i+k-1, n):
if is_pal[i][j]:
ans +=1 i = j +1 found =Truebreakifnot found:
i +=1return ans
impl Solution {
pubfnmax_palindromes(s: String, k: i32) -> i32 {
let n = s.len();
let s = s.as_bytes();
let k = k asusize;
letmut is_pal =vec![vec![false; n]; n];
for len in1..=n {
for l in0..=n-len {
let r = l + len -1;
if s[l] == s[r] && (len <=2|| is_pal[l+1][r-1]) {
is_pal[l][r] =true;
}
}
}
letmut ans =0;
letmut i =0;
while i + k <= n {
letmut found =false;
for j in i+k-1..n {
if is_pal[i][j] {
ans +=1;
i = j +1;
found =true;
break;
}
}
if!found {
i +=1;
}
}
ans
}
}