Minimum Edit Distance Between Two Palindromes
StringsAuto-graded · 19 test casesLast updated
Tests understanding of edit distance (insert, delete, substitute) by computing the minimum operations needed to transform one palindrome into another, reinforcing dynamic programming on strings.
Minimum Edit Distance Between Two Palindromes
You are given two strings, s and t, each of which is guaranteed to be a palindrome.
Your task is to compute the minimum edit distance (also known as Levenshtein distance) required to transform s into t, using only the following three operations, each with a cost of 1:
- Insert a single character anywhere in the string.
- Delete a single character from the string.
- Substitute one character for another at any position.
Because both strings are palindromes, intermediate states during the transformation do not need to be palindromes — only the start (s) and the end result (which must equal t) matter.
Return the minimum total cost to transform s into t.
Constraints
1 ≤ len(s), len(t) ≤ 1000- Both
sandtconsist only of lowercase English letters (a–z). - Both
sandtare valid palindromes (you may assume this without validation). - The answer fits in a standard 32-bit integer.
Examples
Example 1
Input: min_palindrome_edit_distance('racecar', 'racecar')
Output: 0
Example 2
Input: min_palindrome_edit_distance('aba', 'aabaa')
Output: 2
Example 3
Input: min_palindrome_edit_distance('abacaba', 'aca')
Output: 4
Hints
Hint 1
Think about how the classic Levenshtein (edit distance) DP works on two arbitrary strings — the palindrome property doesn't change the algorithm, but it affects which interesting test cases arise (e.g., odd vs. even length palindromes, single-character strings).
Hint 2
Set up a 2D DP table where dp[i][j] = minimum edits to convert s[:i] into t[:j]. Fill it row by row: if characters match, carry the diagonal; otherwise take 1 + min(diagonal, left, above).
Hint 3
The palindrome constraint means certain patterns recur (repeated characters, mirror structure), so watch out for cases where a substitution saves two insertions/deletions or vice versa — the DP handles all of this automatically.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Core Insight
The palindrome property of s and t is a flavor constraint that shapes interesting test cases (mirrored characters, repeated centers, odd vs. even lengths) but does not change the algorithm. The minimum number of single-character edits between two arbitrary strings is exactly the Levenshtein distance, computable with classic 2-D DP.
Algorithm
Define dp[i][j] as the minimum edit cost to convert s[:i] into t[:j].
Base cases:
dp[i][0] = i— delete allicharacters ofs.dp[0][j] = j— insert alljcharacters to buildt[:j]from empty.
Recurrence:
- If
s[i-1] == t[j-1]:dp[i][j] = dp[i-1][j-1](characters match, no cost). - Otherwise:
dp[i][j] = 1 + min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1])dp[i-1][j-1]→ substitutes[i-1]witht[j-1]dp[i-1][j]→ deletes[i-1]dp[i][j-1]→ insertt[j-1]
The answer is dp[m][n].
Why It Is Correct
This is the standard proof-by-induction argument for Levenshtein DP: every edit sequence corresponds to a path through the DP table, and the recurrence captures all minimal paths. The palindrome structure introduces no additional constraints on the operations, only on the endpoints.
Complexity
| Value | |
|---|---|
| Time | O(m · n) |
| Space | O(m · n) — reducible to O(min(m,n)) with rolling arrays |
where m = len(s) and n = len(t).
Reference Solution
def min_palindrome_edit_distance(s: str, t: str) -> int:
m, n = len(s), len(t)
# dp[i][j] = min edits to convert s[:i] -> t[:j]
dp = [[0] * (n + 1) for _ in range(m + 1)]
# Base cases: transforming to/from empty string
for i in range(m + 1):
dp[i][0] = i # delete all i chars of s
for j in range(n + 1):
dp[0][j] = j # insert all j chars of t
for i in range(1, m + 1):
for j in range(1, n + 1):
if s[i - 1] == t[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = 1 + min(
dp[i - 1][j - 1], # substitute
dp[i - 1][j], # delete from s
dp[i][j - 1], # insert into s
)
return dp[m][n]