Minimum Edit Distance to Make String a Palindrome
StringsAuto-graded · 17 test casesLast updated
Tests whether a candidate can compute the minimum number of single-character insertions needed to transform an arbitrary string into a palindrome using interval DP.
Minimum Insertions to Form a Palindrome
Given a string s, return the minimum number of character insertions needed to make s a palindrome.
In a single insertion you may place any character at any position in the string.
How it works
- You may only insert characters (no deletions, no substitutions).
- You want the resulting string to read the same forwards and backwards.
- Return the smallest number of insertions that achieves this.
Key observations
- An empty string and a single character are already palindromes (answer is
0). - If the first and last characters of
smatch, no extra insertion is needed at the outermost level — you only need to fix the interior. - If they differ, one insertion is required to balance the mismatch, then you recurse on the best sub-problem.
Constraints
0 ≤ len(s) ≤ 2000scontains only lowercase English letters ('a'–'z').- An empty string is already a palindrome; return
0.
Examples
Example 1
Input: min_insertions_palindrome('abca')
Output: 1
Example 2
Input: min_insertions_palindrome('racecar')
Output: 0
Example 3
Input: min_insertions_palindrome('')
Output: 0
Hints
Hint 1
Think about the relationship between this problem and the Longest Palindromic Subsequence — the answer is len(s) minus the length of the longest palindromic subsequence.
Hint 2
Try an interval DP on substrings: let dp[i][j] = minimum insertions to make s[i..j] a palindrome. Base cases: dp[i][i] = 0 (single char) and dp[i][i-1] = 0 (empty).
Hint 3
Recurrence: if s[i] == s[j], then dp[i][j] = dp[i+1][j-1]; otherwise dp[i][j] = 1 + min(dp[i+1][j], dp[i][j-1]).
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
The minimum number of insertions to make a string a palindrome equals len(s) - LPS(s), where LPS(s) is the length of the Longest Palindromic Subsequence. Every character not in the LPS must be "mirrored" by inserting its counterpart. However, we solve it directly with interval DP for clarity and efficiency.
Interval DP
Define dp[i][j] as the minimum insertions needed to make the substring s[i..j] a palindrome.
Base cases:
dp[i][i] = 0— a single character is already a palindrome.- For length-2 substrings handled naturally by the recurrence.
Recurrence (fill by increasing substring length):
- If
s[i] == s[j]:dp[i][j] = dp[i+1][j-1]
The outer characters already match; fix only the interior. - If
s[i] != s[j]:dp[i][j] = 1 + min(dp[i+1][j], dp[i][j-1])
We must insert one character to match either the left or right end, then recurse on the cheaper sub-problem.
Correctness
The recurrence is exhaustive: at every step, either the endpoints already match (free), or we pay 1 to add a mirror for one endpoint and optimally solve the remaining substring. Trying both choices and taking the minimum guarantees the global optimum by induction on substring length.
Complexity
- Time: O(n²) — we fill an n×n DP table, each cell in O(1).
- Space: O(n²) for the DP table. (Can be reduced to O(n) with a rolling array if needed.)
Reference Solution
def min_insertions_palindrome(s: str) -> int:
n = len(s)
if n <= 1:
return 0
# dp[i][j] = min insertions to make s[i..j] a palindrome
dp = [[0] * n for _ in range(n)]
for length in range(2, n + 1):
for i in range(n - length + 1):
j = i + length - 1
if s[i] == s[j]:
dp[i][j] = dp[i + 1][j - 1]
else:
dp[i][j] = 1 + min(dp[i + 1][j], dp[i][j - 1])
return dp[0][n - 1]
Worked Example
s = "abca" (n = 4):
| length | i | j | s[i] | s[j] | dp[i][j] |
|---|---|---|---|---|---|
| 2 | 0 | 1 | a | b | 1 |
| 2 | 1 | 2 | b | c | 1 |
| 2 | 2 | 3 | c | a | 1 |
| 3 | 0 | 2 | a | c | 1+min(dp[1][2], dp[0][1])=1+1=2 → wait: 1+min(1,1)=2? Let's recheck: dp[1][2]=1, dp[0][1]=1 → 2. But s[0]='a', s[2]='c' differ → 1+min(dp[1][2],dp[0][1])=1+min(1,1)=2 |
| 3 | 1 | 3 | b | a | 1+min(dp[2][3], dp[1][2])=1+min(1,1)=2 |
| 4 | 0 | 3 | a | a | dp[1][2]=1 |
Answer: dp[0][3] = 1. Insert one 'b' (or 'c') to get e.g. "abcba" — correct!