Given 2 strings, find the minimum number of operations required to change one string to the other. An operation is defined as either replacement of a character, deletion of a character, or addition of a character. This problem is also called Levenshtein Distance. Levenshtein distance is a string metric for measuring the difference between two sequences.
Input:
str1 = 'abc'
str2 = 'yabd'
Output: 2
To convert abc
to yabd
, there are two ways:
y
to string abc
at position str[0]
, replace character c
to characterd
at position str1[2]
y
at position str2[0]
, replace character d
with character c
at position str2[3]
.Input:
1 <= str1.length <= 100
1 <= str2.length <= 100
Click to reveal
Try creating a 2D matrix to store results. Is there a way to remember what were the previous computations?
Click to reveal
This is a classic Dynamic Programming question. Can you try taking the Minimum or Maximum values of the array that you're going to make?