Minimum Single-Character Edits to Make a Palindrome
StringsAuto-graded · 18 test casesLast updated
Tests whether a candidate can count the minimum number of single-character insertions or deletions needed to transform a string into any palindrome.
Minimum Single-Character Edits to Make a Palindrome
Given a string s, you may perform two kinds of edit operations, each costing 1:
- Insert any single character at any position in the string.
- Delete any single character from the string.
Return the minimum total number of edits required to turn s into a palindrome.
A palindrome reads the same forwards and backwards (e.g.
"racecar","abba","a","").
Note: Substitution is not allowed — only insertions and deletions count.
How to think about it
Consider which characters already "match" across the two ends of the string. The longest palindromic subsequence (LPS) of s is the largest set of characters you can keep without any edits; every other character must be removed or paired with an inserted mirror. The answer is therefore len(s) − LPS(s).
Constraints
0 ≤ len(s) ≤ 1000scontains only lowercase English letters (a–z).- The empty string
""is already a palindrome; return0. - A single character is already a palindrome; return
0.
Examples
Example 1
Input: min_edits_to_palindrome('race')
Output: 3
Example 2
Input: min_edits_to_palindrome('abcba')
Output: 0
Example 3
Input: min_edits_to_palindrome('')
Output: 0
Hints
Hint 1
Think about which characters you can keep: the longest subsequence of s that is already a palindrome needs no edits. Everything else requires one edit per character.
Hint 2
The Longest Palindromic Subsequence (LPS) of s equals the Longest Common Subsequence (LCS) of s and reverse(s). Build a 2-D DP table of size (n+1) × (n+1).
Hint 3
The answer is len(s) − LPS(s), because each character outside the LPS needs exactly one insertion or deletion to fix the palindrome property.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
A palindrome can keep any subsequence of s that is itself a palindrome — those characters need zero edits. Every character not in the longest such subsequence must eventually be removed (1 delete each) or paired via an insertion (1 insert each). Either way the cost is exactly 1 per excluded character.
Therefore:
answer = len(s) − length_of_longest_palindromic_subsequence(s)
Computing the Longest Palindromic Subsequence (LPS)
A classic equivalence: the LPS of s equals the Longest Common Subsequence (LCS) of s and its reverse t = s[::-1].
Why? Any palindrome subsequence of s appears in the same character positions in both s and t, so it is a common subsequence. Conversely, any common subsequence of s and t can be interpreted as a palindrome.
DP Table
Build dp[i][j] = length of LCS of s[:i] and t[:j]:
- If
s[i-1] == t[j-1]:dp[i][j] = dp[i-1][j-1] + 1 - Otherwise:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
dp[n][n] gives the LPS length.
Correctness
- Empty string →
n = 0→ return0. ✓ - Single character → LPS = 1 →
1 − 1 = 0. ✓ "ab"→ LPS = 1 (either"a"or"b") →2 − 1 = 1(one insert/delete). ✓"abcba"→ already a palindrome → LPS = 5 →0. ✓
Complexity
| Value | |
|---|---|
| Time | O(n²) |
| Space | O(n²) |
Both are well within budget for n ≤ 1000 (≈10⁶ operations, ≈10⁶ cells).
Reference Solution
def min_edits_to_palindrome(s: str) -> int:
n = len(s)
if n == 0:
return 0
# LPS(s) = LCS(s, reverse(s))
t = s[::-1]
# dp[i][j] = length of LCS of s[:i] and t[:j]
dp = [[0] * (n + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
for j in range(1, n + 1):
if s[i - 1] == t[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
lps = dp[n][n]
return n - lps