- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find minimum deletions to make strings strings in Python

Suppose we have two lowercase strings s and t, now consider an operation where we can delete any character in any of these two strings. We have to find the minimum number of operations needed to make s and t equal.

So, if the input is like s = "pipe" t = "ripe", then the output will be 2, because we can delete "p" from s and "r" from t to make these strings same "ipe"

To solve this, we will follow these steps −

- m := size of s
- n := size of t
- Define a function dp() . This will take i, j
- if i is same as m, then
- return n - j

- otherwise when j is same as n, then
- return m - i

- otherwise,
- if s[i] is same as t[j], then
- return dp(i + 1, j + 1)

- otherwise,
- return 1 + (minimum of dp(i + 1, j) and dp(i, j + 1))

- if s[i] is same as t[j], then
- From the main method, return dp(0, 0)

## Example

Let us see the following implementation to get better understanding −

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" print(solve(s, t))

## Input

"pipe", "ripe"

## Output

2

- Related Questions & Answers
- Program to find minimum deletions to make string balanced in Python
- Minimum Swaps to Make Strings Equal in C++
- Program to count minimum deletions needed to make character frequencies unique in Python
- Minimum Cost To Make Two Strings Identical in C++
- Program to find minimum number of deletions required from two ends to make list balanced in Python
- Using Counter() in Python 3.x. to find minimum character removal to make two strings anagram
- Find the minimum number of preprocess moves required to make two strings equal in Python
- Program to split two strings to make palindrome using Python
- Minimum Cost to make two Numeric Strings Identical in C++
- Minimum Number of Steps to Make Two Strings Anagram in C++
- Minimum move to end operations to make all strings equal in C++
- Program to find the minimum edit distance between two strings in C++
- Minimum number of deletions to make a string palindrome in C++.
- Python program to find uncommon words from two Strings
- Program to find largest merge of two strings in Python

Advertisements