Problem Statement
Write a program to validate how many edits (add, remove, or replace character) are required to convert a given string into another given string. You need to consider that comparison is case sensitive and whitespace is significant.
Example 1
Input: s1 = "bat" s2 = "bad"
Output
Only one edit is required to convert one string to another.
Explanation
One edit (character 't' needs to be replaced by 'd') is required to convert s1 into s2.
Example 2
Input: s1 = "belt" s2 = "bel"
Output
Only one edit is required to convert one string to another
Explanation
One edit (character 't' needs to be removed or deleted) is required to convert s1 into s2.
Example 3
Input: s1 = "sea" s2 = "seat"
Output
Only one edit is required to convert one string to another
Explanation
one edit (character 't' needs to be added) is required to convert s1 into s2.
Example 4
Input: s1 = "salt" s2 = "sa"
Output
more than one edit
Explanation
more than one edit (character 'l' and 't' need to be removed) is required to convert s1 into s2.
Example 5
Input: s1 = "Salt" s2 = "sal"
Output
More than one edit
Explanation
More than one edit (character 't' needs to be removed and 'S' need to be replaced by 's') is required to convert s1 into s2.
Example 6
Input: s1 = "sea food" s2 = "seafoo"
Output
more than one edit
Explanation
more than one edit (character 'd' needs to be removed and ' ' need to be removed ) is required to convert s1 into s2.
Solution
Look at the steps described for an algorithm
- Compare the length of two strings and if the difference between these two strings is more than one character then print the result “more than one edit is required to convert one string to another” because to convert one string to another string more than one edits (add, remove, or replace character) are required.
- Find out a longer and smaller string based on the length difference, if the difference is less than 0 then the second given string is the longer string OR if the difference is more than 0 then first given string is the longer string OR difference equals to 0 then both strings are of the same length
- Convert both the given strings into an array of a type character
- Declare a variable to store the count of edits required
- Iterate through string character set while loop reaches the end of longer string length or end of smaller string length,
- check the value of variable edit counter, if more than one edit is found then return failure signal and print the result “more than one edit is required to convert one string to another”
- match the character of given strings (longer and smaller)
- if character matches then increment the counter for both smaller and longer string index otherwise,
- if the character does not match then do the following,
- increment the index counter for longer string and
- Increment the index counter for smaller string if and only if the length of smaller and longer is same
- increment the edit counter
- finally, print “only one edit is required to convert one string to another”

Join the conversation! Your thoughts help the community grow.