Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Minimum Number of Deletions and Insertions
MEDIUM
12 upvotes
Dynamic Programming
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Dynamic Programming
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Minimum Number of Deletions and Insertions

Contributed by
KAPEESH UPADHYAY
Medium
yellow-spark
0/80
Share
12 upvotes

Problem Statement

You are given 2 non-empty strings “str” and “ptr” consisting of lowercase English alphabets only. Your task is to convert string “str” into string “ptr”. In one operation you can do either of the following on “str”:

Remove a character from any position in “str”.

Add a character to any position in “str”.

You have to find the minimum number of operations required to convert string “str” into “ptr”.

For Example:
If str = “abcd”, ptr = “anc”
In one operation remove str[3], after this operation str becomes “abc”.    
In the second operation remove str[1], after this operation str becomes “ac”.
In the third operation add ‘n’ in str[1], after this operation str becomes “anc”.

Hence, the output will be 3.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1 :
2
abcd anc
aa aaa
Sample output 1 :
3
1
Explanation For Sample Output 1:
For the first test case,
str = “abcd”, ptr = “anc”

In one operation remove str[3], after this operation str becomes “abc”.
In the second operation remove str[1], after this operation str becomes “ac”.
In the third operation add ‘n’ in str[1], after this operation str becomes “anc”.

Hence, the output will be 3.

For the second test case,
str = “aaa”, ptr = “aa”

In one operation remove str[2], after this operation str becomes “aa”.

Hence, the output will be 1.
Sample Input 2 :
2
cue dgo
edl xcqja
Sample output 2 :
6
8
Reset Code
Full screen
Auto
copy-code
Console