Table of Contents#
- Problem Statement
- Understanding the Approach
- Dynamic Programming Solution
- Algorithm Explanation
- Python Code Example
- Complexity Analysis
- Common Practices and Best Practices
- Example Usage
- Conclusion
- References
Problem Statement#
Given a string s, the goal is to find the minimum number of character deletions needed to convert the string into a palindrome. For example, if the input string is "abcba", it is already a palindrome, so the minimum number of deletions is 0. If the input string is "abcd", the minimum number of deletions is 3, as we can delete any three characters to get a single - character palindrome.
Understanding the Approach#
The key idea behind solving this problem is to use the concept of the Longest Common Subsequence (LCS). We know that if we reverse the given string s to get s_reversed, the length of the LCS between s and s_reversed represents the longest palindromic subsequence of the original string s.
The minimum number of deletions required to make the string a palindrome is equal to the length of the string minus the length of its longest palindromic subsequence. This is because the longest palindromic subsequence is the part of the string that is already "palindrome - like", and the remaining characters need to be deleted.
Dynamic Programming Solution#
Algorithm Explanation#
- Reverse the String: First, reverse the input string
sto gets_reversed. - Find the LCS: Use a dynamic programming approach to find the length of the LCS between
sands_reversed.- Create a two - dimensional array
dpof size(n + 1) x (n+ 1), wherenis the length of the strings.dp[i][j]will store the length of the LCS of the firsticharacters ofsand the firstjcharacters ofs_reversed. - Initialize
dp[0][j] = 0anddp[i][0]=0for alliandjfrom0ton. - For
ifrom1tonandjfrom1ton:- If
s[i - 1]==s_reversed[j - 1], thendp[i][j]=dp[i - 1][j - 1]+1. - Otherwise,
dp[i][j]=max(dp[i - 1][j], dp[i][j - 1]).
- If
- Create a two - dimensional array
- Calculate the Minimum Deletions: The minimum number of deletions is
n - dp[n][n], wherenis the length of the strings.
Python Code Example#
def min_deletions_to_palindrome(s):
n = len(s)
s_reversed = s[::-1]
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] == s_reversed[j - 1]:
dp[i][j] = dp[i - 1][j - 1]+1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return n - dp[n][n]
# Example usage
string = "abcba"
print(min_deletions_to_palindrome(string))Complexity Analysis#
- Time Complexity: The time complexity of this algorithm is $O(n^{2})$, where
nis the length of the strings. This is because we have two nested loops, each runningntimes. - Space Complexity: The space complexity is also $O(n^{2})$ due to the two - dimensional
dparray.
Common Practices and Best Practices#
- Initialization: Always initialize the
dparray properly. In this case, settingdp[0][j] = 0anddp[i][0]=0is crucial as it represents the base case when one of the two strings has a length of 0. - Code Readability: Use descriptive variable names like
s_reversedanddpto make the code easier to understand and maintain. - Error Handling: Consider adding input validation to handle edge cases such as empty strings.
Example Usage#
Let's consider a few more examples:
strings = ["abcd", "racecar", "abba"]
for string in strings:
print(f"String: {string}, Minimum deletions: {min_deletions_to_palindrome(string)}")For the string "abcd", the minimum number of deletions is 3. For the string "racecar", the minimum number of deletions is 0 as it is already a palindrome. For the string "abba", the minimum number of deletions is also 0.
Conclusion#
In this blog post, we have explored an efficient dynamic programming solution to find the minimum number of deletions required to make a string a palindrome. By using the concept of the Longest Common Subsequence, we were able to reduce the problem to a well - known dynamic programming problem. This approach has a time complexity of $O(n^{2})$, which is a significant improvement over some naive brute - force solutions.
References#
- Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein
- GeeksforGeeks - https://www.geeksforgeeks.org/minimum-number-deletions-make-string-palindrome/
- LeetCode - https://leetcode.com/