Problem
Given a string s
, in one step you can either insert or delete any character at any index of the string. Return the minimum number of insertions or deletions required to make s
a palindrome.
A Palindrome String is one that reads the same backward as well as forward.
Examples
Example 1:
|
|
Example 2:
|
|
Example 3:
|
|
Similar Problems
Minimum Insertion Steps to Make a String Palindrome Minimum Deletion Steps to Make a String Palindrome
Solution
Method 1 – Recursion
To find the minimum number of insertions or deletions needed to make a substring a palindrome, we use a recursive approach:
- If the characters at the current left and right positions are equal, move inward and solve for the substring.
- If not, try inserting or deleting a character at either end and take the minimum steps required for the resulting substrings, plus one for the operation.
Recurrence
Let O[i..j]
be the minimum number of operations needed to make the substring from index i
to j
a palindrome:
O[i..j] = O[i+1..j-1]
ifs[i] == s[j]
O[i..j] = min(O[i..j-1], O[i+1..j]) + 1
otherwise
Base Case:
O[i,i] = 0
(a single character is already a palindrome)
C++
|
|
Java
|
|
Python
|
|
Complexity
- ⏰ Time complexity:
O(2^N)
, because each character can be either included or excluded, leading to exponential subproblems. - 🧺 Space complexity:
O(N)
, for the recursion stack.
Method 2 – Top Down DP (Memoization)
Intuition
The top-down DP approach uses recursion with memoization to avoid recomputing results for the same substring. By storing results for each substring, we efficiently solve overlapping subproblems and reduce the exponential time complexity of the naive recursive solution.
Approach
- Use a helper function that takes two indices,
i
andj
, representing the current substring. - If
i >= j
, return 0 (base case: single character or empty substring is a palindrome). - If the result for
(i, j)
is already computed, return it from the memo table. - If
s[i] == s[j]
, recursively solve for the inner substring (i+1, j-1
). - Otherwise, try inserting or deleting at either end and take the minimum of both options plus one.
- Store the result in the memo table and return it.
Java
|
|
C++
|
|
Python
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
, because there are O(n^2) unique substring pairs and each is computed once due to memoization. - 🧺 Space complexity:
O(n^2)
, for the memoization table storing results for all substring pairs.
Method 3 – Dynamic Programming (Edit Distance without Replace)
Intuition
To make a string palindrome using the minimum number of insertions or deletions, we can model the problem as transforming the string into its reverse using only insert and delete operations (no replacements). This is similar to the edit distance problem, but without the replace operation. The minimum number of operations required is the difference between the length of the string and the length of its Longest Common Subsequence (LCS) with its reverse. The LCS represents the largest part of the string that is already a palindrome, and the remaining characters must be inserted or deleted to achieve a palindrome.
For example, consider S = “ababcac” and its reverse S’ = “cacbaba”. We can transform S into S’ as follows:
|
|
So we need 4 operations to transform S into S’ and hence into a palindrome.
Approach
To solve this problem, we use dynamic programming to find the length of the Longest Common Subsequence (LCS) between the string and its reverse. The LCS represents the largest palindromic part of the string. The minimum number of insertions or deletions required is the difference between the length of the string and the length of its LCS with its reverse.
Here are the steps:
- Reverse the input string to get
rev
. - Initialize a DP table
dp
of size(n+1) x (n+1)
wheren
is the length of the string. - For each character position
i
in the original string andj
in the reversed string:- If
s[i-1] == rev[j-1]
, setdp[i][j] = dp[i-1][j-1] + 1
. - Otherwise, set
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
.
- If
- After filling the DP table, the value at
dp[n][n]
gives the length of the LCS. - The answer is
n - dp[n][n]
, which is the minimum number of insertions or deletions needed to make the string a palindrome.
Code
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
— We fill a DP table for all substring pairs. - 🧺 Space complexity:
O(n^2)
— The DP table stores results for all substring pairs.