Sum palindrome-change costs over all substrings
Problem Description
You are given a DNA string dna consisting only of characters A, C, G, T.
For any substring dna[l..r], its palindrome modification cost is defined as the minimum number of single-character substitutions required to make that substring a palindrome. This is equivalent to counting the number of mismatched character pairs when comparing from both ends: specifically, the count of indices i where dna[l+i] != dna[r-i] for i < (r-l+1)/2.
Your task is to compute the sum of palindrome modification costs over all substrings of dna, and output the result as a 64-bit integer.
Input
- A single string
dna.
Output
- A single integer: the sum of costs across all substrings.
Notes
- You may assume the result fits in a signed 64-bit integer (
long/int64).
AI interview prep
Core dilemma
The central engineering conflict is efficiently calculating the palindrome modification cost for O(N^2) substrings. A naive O(N) check for each substring leads to an O(N^3) total complexity, which is unacceptable for typical string lengths. The dilemma is to reduce this to an O(N^2) or better solution by leveraging overlapping subproblems.
2 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Sum palindrome-change costs over all substrings
Problem Description
You are given a DNA string dna consisting only of characters A, C, G, T.
For any substring dna[l..r], its palindrome modification cost is defined as the minimum number of single-character substitutions required to make that substring a palindrome. This is equivalent to counting the number of mismatched character pairs when comparing from both ends: specifically, the count of indices i where dna[l+i] != dna[r-i] for i < (r-l+1)/2.
Your task is to compute the sum of palindrome modification costs over all substrings of dna, and output the result as a 64-bit integer.
Input
- A single string
dna.
Output
- A single integer: the sum of costs across all substrings.
Notes
- You may assume the result fits in a signed 64-bit integer (
long/int64).