Maximum Number of Non-overlapping Palindrome Substrings - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
s = "abccba", k = 3
›
Output:
1
💡 Note:
We can select "bccb" (indices 1-4) or "abccba" (indices 0-5), both are palindromes of length ≥ 3. Maximum is 1.
Example 2 — Multiple Palindromes
$
Input:
s = "ababa", k = 3
›
Output:
1
💡 Note:
We have palindromes "aba" (0-2) and "aba" (2-4), but they overlap at index 2. We can only select one of them. Maximum is 1.
Example 3 — No Valid Palindromes
$
Input:
s = "abc", k = 4
›
Output:
0
💡 Note:
String length is 3, but we need palindromes of length ≥ 4. No valid palindromes exist.
Constraints
- 1 ≤ s.length ≤ 2000
- s consists of lowercase English letters only
- 1 ≤ k ≤ s.length
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code