Maximum Number of Non-overlapping Palindrome Substrings
Real-world version: Cutting Mirror Pieces From the RibbonThe statement here is our own: the same inputs, outputs and technique.
A ribbon ribbon holds lowercase letters. A piece is a stretch of neighbouring letters that reads the same forwards and backwards and is at least k letters long.
Cut out as many pieces as possible so that no two of them share a letter of the ribbon.
Return the largest number of pieces that can be cut.
Examples
Example 1
- Input
- ribbon = "aaaa", k = 2
- Output
- 2
Cut the first two letters and then the last two. Three pieces of two letters could never fit inside four letters.
Example 2
- Input
- ribbon = "aba", k = 2
- Output
- 1
No two neighbouring letters match, so the only stretch reading the same both ways and long enough is the whole ribbon.
Example 3
- Input
- ribbon = "ab", k = 2
- Output
- 0
The two letters differ, so the ribbon holds no two-letter stretch reading the same both ways.
Constraints
- 1 <= k <= ribbon.length <= 2000
- ribbon holds only lowercase English letters
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def max_palindromes(ribbon: str, k: int) -> int:public int maxPalindromes(String ribbon, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Find the Lexicographically Smallest Valid SequenceReal-world version: Cheapest Probe Slots On A ReelMedium
- Valid Palindrome IIReal-world version: Mosaic Border InspectionEasy
- Is SubsequenceReal-world version: Shorthand Fits the LabelEasy
- Longest Palindromic SubstringReal-world version: Longest Mirror RunMedium