Fewest Edits to a Repeating Palindromic Pattern
StringsAuto-graded · 25 test casesLast updated
Find the minimum character edits so a string becomes both a palindrome and a repetition of a length-k block, by grouping positions into forced-equal classes.
You are given a lowercase string text of length n and a positive integer period, where n is an exact multiple of period.
In one edit you may replace any single character of text with any lowercase letter. You want to end up with a string result of the same length that satisfies both rules:
- Palindrome:
result[j] == result[n - 1 - j]for every validj. - Repeating pattern:
result[j] == result[j + period]for everyjwherej + period < n. In other words,resultis one block ofperiodcharacters repeatedn / periodtimes.
Return the smallest number of edits needed to turn text into such a string.
Notes
- Only the number of edits is returned, not the resulting string.
- A string that already satisfies both rules needs
0edits. - When
periodis odd, the middle position of each block mirrors onto itself, so it forms a group on its own and must not be counted twice.
Constraints
2 <= n <= 2 * 10^51 <= period < nn % period == 0textcontains only lowercase English letters (a-z).
Examples
Example 1
Input: min_edits_periodic_palindrome('abca', 2)
Output: 2
Example 2
Input: min_edits_periodic_palindrome('abcabc', 3)
Output: 2
Example 3
Input: min_edits_periodic_palindrome('aabbcc', 2)
Output: 4
Hints
Hint 1
The repeating rule means every position with the same index modulo `period` must end up with the same letter. Start by thinking of each residue as one unit.
Hint 2
Because `n` is a multiple of `period`, the mirror of position `j` has residue `period - 1 - (j % period)`. So residue `r` and residue `period - 1 - r` must also match, which merges residues into pairs (or a single self-paired middle residue when `period` is odd).
Hint 3
Each merged group is independent of the others. Inside a group, keep the most frequent letter and edit the rest, so the group costs `group_size - max_letter_count`. Sum this over all groups.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Step 1: periodicity. The rule result[j] == result[j + period] forces all positions with the same value of j % period to hold the same letter. So result is determined by a single block of period letters repeated.
Step 2: palindrome on top of periodicity. Position j mirrors to n - 1 - j. Since n is a multiple of period, the residue of the mirror is period - 1 - (j % period). So residue r must carry the same letter as residue period - 1 - r.
Step 3: groups. For each r from 0 to ceil(period / 2) - 1, the group is every position whose residue is r or period - 1 - r. When period is odd, the middle residue pairs with itself and is counted once. These groups partition all positions, and nothing links two different groups.
Step 4: greedy per group. Every group must end up as one letter. If the group has S positions and its most common letter appears M times, choosing that letter costs S - M edits, which is the minimum for that group. The answer is the sum over all groups.
Why it is correct
- Necessary: any valid result must be constant on every group, by the two rules above.
- Sufficient: if every group is constant, the result is a palindromic block repeated. The reverse of that string is the reversed block repeated, which is the same string. So both rules hold.
- Optimal: the groups are disjoint and independent, so minimizing each one separately minimizes the total.
Complexity
- Time:
O(n + 26 * period), which isO(n)becauseperiod < n. - Space:
O(26 * period)for the per-residue letter counts.
Reference solution
from typing import List
def min_edits_periodic_palindrome(text: str, period: int) -> int:
# counts[r][c] = how many times letter c appears at positions with index % period == r
counts: List[List[int]] = [[0] * 26 for _ in range(period)]
for i, ch in enumerate(text):
counts[i % period][ord(ch) - ord('a')] += 1
edits = 0
for r in range((period + 1) // 2):
mirror = period - 1 - r
if r == mirror:
combined = counts[r]
else:
combined = [a + b for a, b in zip(counts[r], counts[mirror])]
edits += sum(combined) - max(combined)
return edits