Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find minimum deletions to make strings strings in Python
Sometimes we need to find the minimum number of deletions required to make two strings equal. This is a classic dynamic programming problem that can be solved by finding the Longest Common Subsequence (LCS) and subtracting it from the total length of both strings.
Given two lowercase strings s and t, we need to delete characters from either string to make them identical. The key insight is that we want to keep the maximum number of common characters and delete the rest.
Problem Example
If we have s = "pipe" and t = "ripe", we can delete "p" from s and "r" from t to get "ipe" in both strings, requiring 2 deletions.
Algorithm Approach
We use a recursive function with the following logic ?
- If we reach the end of string
s, delete remaining characters fromt - If we reach the end of string
t, delete remaining characters froms - If characters match, move to next position in both strings
- If characters don't match, try deleting from either string and take minimum
Implementation
def solve(s, t):
m = len(s)
n = len(t)
def dp(i, j):
if i == m:
return n - j
elif j == n:
return m - i
else:
if s[i] == t[j]:
return dp(i + 1, j + 1)
else:
return 1 + min(dp(i + 1, j), dp(i, j + 1))
return dp(0, 0)
s = "pipe"
t = "ripe"
result = solve(s, t)
print(f"Minimum deletions needed: {result}")
Minimum deletions needed: 2
How It Works
The algorithm explores all possible ways to align the strings ?
- When
s[i] == t[j]: Characters match, so we keep both and move forward - When
s[i] != t[j]: We must delete one character, so we try both options and pick the minimum - Base cases handle when one string is exhausted
Optimized Version with Memoization
For better performance with larger strings, we can add memoization ?
def solve_optimized(s, t):
m, n = len(s), len(t)
memo = {}
def dp(i, j):
if (i, j) in memo:
return memo[(i, j)]
if i == m:
result = n - j
elif j == n:
result = m - i
elif s[i] == t[j]:
result = dp(i + 1, j + 1)
else:
result = 1 + min(dp(i + 1, j), dp(i, j + 1))
memo[(i, j)] = result
return result
return dp(0, 0)
# Test with different examples
test_cases = [
("pipe", "ripe"),
("abc", "def"),
("hello", "world")
]
for s, t in test_cases:
result = solve_optimized(s, t)
print(f'"{s}" and "{t}" ? {result} deletions')
"pipe" and "ripe" ? 2 deletions "abc" and "def" ? 6 deletions "hello" and "world" ? 8 deletions
Conclusion
This problem is solved by finding the minimum deletions using dynamic programming. The recursive approach tries all possibilities, while memoization optimizes repeated subproblems for better performance.
