Count Character Mismatches Across All Palindrome Mirrors
StringsAuto-graded · 24 test casesLast updated
Tests the ability to reframe a quadratic substring enumeration into a pair-centric counting problem with geometric weight calculations.
You are given a string sequence containing only the characters A, C, G, and T.
For any contiguous substring of sequence, we can measure how far it is from being a palindrome by counting the number of positions that would need to change. Specifically, for a substring of length m, we compare characters at mirror positions: the first with the last, the second with the second-to-last, and so on, stopping at the middle. Each pair of mirror positions that holds different characters contributes 1 to the cost.
Your task is to calculate the total cost across every possible contiguous substring of sequence.
Constraints:
1 ≤ len(sequence) ≤ 5000sequencecontains only the charactersA,C,G,T- The result will fit in a 64-bit signed integer
Examples
Example 1
Input: total_mismatch_cost('ACGT')
Output: 7
Example 2
Input: total_mismatch_cost('AAAA')
Output: 0
Example 3
Input: total_mismatch_cost('ACCA')
Output: 4
Example 4
Input: total_mismatch_cost('AGCTAG')
Output: 20
Hints
Hint 1
Instead of iterating over all substrings and checking each one, think about the contribution of each pair of positions in the original string.
Hint 2
For two positions i and j where sequence[i] != sequence[j], count how many substrings include both positions as a mirror pair.
Hint 3
A pair at positions i and j forms a mirror in a substring [L, R] when they are equidistant from the substring's center: i - L == R - j.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The naive approach of examining every substring individually leads to O(n³) time complexity: O(n²) substrings, each requiring O(n) time to count mismatches. For strings up to 5000 characters, this is too slow.
The key insight is to reverse the question: instead of asking "for each substring, how many mismatches does it contain?", ask "for each pair of positions, how many substrings include this pair as a mirror pair?"
Two positions i and j (where i < j) form a mirror pair in substring [L, R] when they are equidistant from the boundaries: i - L == R - j. Rearranging: L = i + j - R. For this to be valid:
L ≥ 0, which givesR ≥ i + jL ≤ i(left boundary doesn't go past position i)R ≥ j(right boundary includes position j)R < n(stays within the string)
Combining these: max(j, i + j) ≤ R < n, which simplifies to i + j ≤ R < n.
The left boundary is determined by R: L = i + j - R. The constraint L ≥ 0 gives R ≤ i + j, but we already have R ≥ i + j, so the tightest lower bound is R = i + j. The constraint L ≤ i gives i + j - R ≤ i, so R ≥ j, which is already satisfied.
Therefore, valid values of R range from max(i + j, n - 1) down to i + j. Wait, let me recalculate: we need i + j ≤ R < n and L = i + j - R ≥ 0, giving R ≤ i + j. Combined: R = i + j is the only value where both constraints on L hold when i + j < n.
Actually, the correct derivation: for each R in [i+j, n-1], we get L = i + j - R. We need 0 ≤ L ≤ i. From L ≥ 0: R ≤ i + j. From L ≤ i: R ≥ j. So valid R values are [max(j, i+j-i), min(n-1, i+j)] = [j, min(n-1, i+j)]. But since j ≤ i + j when i ≥ 0, the range becomes [i+j, min(n-1, i+j)].
Let me reconsider from the reference solution's perspective: first_left = max(0, i + j - n + 1) and the count is i - first_left + 1. This counts how many valid left boundaries exist. For R = n-1 (rightmost possible), L = i+j-(n-1) = i+j-n+1, which must be ≥ 0. For smaller R values, L increases. The leftmost L can be is 0, giving R = i+j. The rightmost L can be is i (to keep i inside), giving R = j. So L ranges from max(0, i+j-n+1) to i, giving i - max(0, i+j-n+1) + 1 substrings.
Algorithm:
- For each pair of indices (i, j) where i < j
- If the characters differ, calculate how many substrings include them as mirrors
- The count is
i - max(0, i + j - n + 1) + 1 - Sum these contributions
Complexity:
- Time: O(n²) — we examine each pair once
- Space: O(1) — only a running total
This approach handles the large output values correctly by using a 64-bit accumulator, since the result can grow as large as O(n³).
def total_mismatch_cost(sequence: str) -> int:
n = len(sequence)
total = 0
for i in range(n):
for j in range(i + 1, n):
if sequence[i] != sequence[j]:
first_left = max(0, i + j - n + 1)
total += i - first_left + 1
return total