Minimum Edits to Form Palindromic Segments
StringsAuto-graded · 23 test casesLast updated
Tests two-pointer logic and palindrome verification to compute the fewest replacements that transform every fixed-size segment into a palindrome.
You receive a string text made up of lowercase English letters and an integer block_size.
Divide text into consecutive, non-overlapping segments, each exactly block_size characters long. The length of text is guaranteed to be a multiple of block_size, so the division is exact and leaves no remainder.
In one operation, you may replace any single character in text with any other lowercase English letter. Segment boundaries remain fixed throughout.
Return the smallest number of replacements needed so that every segment becomes a palindrome.
Constraints
1 ≤ block_size ≤ len(text) ≤ 200,000len(text) % block_size == 0textcontains only lowercase English letters ('a'through'z')
Examples
Example 1
Input: min_edits_palindrome_segments('abcbadeba', 3)
Output: 3
Example 2
Input: min_edits_palindrome_segments('racecarannaana', 7)
Output: 1
Example 3
Input: min_edits_palindrome_segments('aaaa', 2)
Output: 0
Hints
Hint 1
Each segment can be made palindromic independently; changes in one segment do not affect any other.
Hint 2
Within a single segment of length k, use two pointers starting at opposite ends and moving inward. Count how many mirrored pairs differ.
Hint 3
When the two characters at mirrored positions do not match, exactly one replacement is required to make them equal.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key observation is that segment boundaries are fixed, so segments do not interact. The minimum cost for the entire string is the sum of the minimum cost for each segment treated independently.
Within a single segment of length block_size, a palindrome requires every character at position i to match the character at the mirrored position block_size - 1 - i (using offsets relative to the segment start). These mirrored pairs are independent:
- If the two characters already match, no replacement is needed for that pair.
- If they differ, exactly one replacement makes them equal. We can overwrite either character with the other's value.
- When
block_sizeis odd, the middle character mirrors itself and never requires a change.
The minimum number of replacements for one segment is therefore the count of mismatched mirrored pairs. Summing across all segments gives the answer.
Why It Works
Lower bound: Each mismatched pair uses two distinct positions that must hold the same character in any palindrome. Since pairs are disjoint, one replacement fixes at most one pair. Therefore, every mismatch forces at least one replacement.
Achievability: For each mismatched pair, replace one character with the other. This fixes that pair without affecting any other pair or segment.
The lower bound matches what we achieve, so counting mismatches is optimal.
Complexity
- Time: O(n), where n is the length of
text. We performblock_size // 2comparisons per segment, and there aren / block_sizesegments, yielding roughlyn / 2comparisons total. - Space: O(1) extra space. We only maintain pointers and a counter, avoiding substring allocations.
Implementation
def min_edits_palindrome_segments(text: str, block_size: int) -> int:
replacements = 0
length = len(text)
for segment_start in range(0, length, block_size):
left_ptr = segment_start
right_ptr = segment_start + block_size - 1
while left_ptr < right_ptr:
if text[left_ptr] != text[right_ptr]:
replacements += 1
left_ptr += 1
right_ptr -= 1
return replacements
The outer loop iterates over each segment's starting index. The inner two-pointer loop walks from both ends toward the center, counting mismatched pairs.