Edit distance

problem [https://leetcode.com/problems/edit-distance/ 72. Edit distance].

Problem description
Given two strings  and , return the minimum number of operations required to convert   to.

You have the following three operations permitted on a word:
 * Insert a character
 * Delete a character
 * Replace a character


 * Example 1
 * Input: word1 = "horse", word2 = "ros"
 * Output: 3
 * Explanation:
 * horse → rorse (replace 'h' with 'r')
 * rorse → rose (remove 'r')
 * rose → ros (remove 'e')


 * Example 2
 * Input: word1 = "intention", word2 = "execution"
 * Output: 5
 * Explanation:
 * intention → inention (remove 't')
 * inention → enention (replace 'i' with 'e')
 * enention → exention (replace 'n' with 'x')
 * exention → exection (replace 'n' with 'c')
 * exection → execution (insert 'u')


 * Constraints
 * and  consist of lowercase English letters.
 * and  consist of lowercase English letters.

Defining states
Here are some basic facts we notice:
 * 1) If one input string is empty, then the edit distance between the two strings is the length of the non-empty one.
 * 2) If two input strings end with the same character, then removing the ending character from both strings yields the same edit distance.
 * 3) If two input strings (  and  ) end with different characters, then from the bottom-up perspective, this can be interpreted as:
 * 4) Edit one of the string into the other string minus the last character.
 * 5) Append the last character excluded above to get the other string.

So we can consider  edit distance between   and. This represents each single state in the edit distance table.

Defining transitions

 * If either  or   is empty, then the edit distance is the length of the other non-empty string.
 * In the general case, consider substrings  and   (i, j > 0):
 * If  (i.e. the last characters of the two substrings are the same), then obviously.
 * Otherwise,  may be given by referring to these three smaller subproblems, and picking the minimum of these three results:
 *  : This means that after editing substrings  to , we replace   into   as the last edit.
 *  : This means that after editing substrings  to , we append   as the last edit.
 *  : This means that after editing substrings  to , we append   as the last edit.
 * So the transition formula is:
 * If :
 * Otherwise, minimum of:

Fill in the table

 * Example : ,
 * The steps are as follows:
 * Create a  of size (5+1) rows × (4+1) columns.
 * Fill in the top row with values 0-5, and the left column with values 0-4.
 * Fill in other cells from top left to bottom right. The rules are as follows: For :
 * If, then.
 * Otherwise, pick the min of:
 * Return.
 * The filled table is as follows:
 * Return.
 * The filled table is as follows:
 * The filled table is as follows:


 * style="padding-right: 2em;" |
 * }
 * So.
 * Remove "g".
 * Replace "r" to "f".
 * Replace the last "s" to "t".
 * Replace "r" to "f".
 * Replace the last "s" to "t".

Complexity analysis

 * Time complexity
 * Let m, n be the lengths of  and   respectively.
 * Then the  table has size (m+1) × (n+1).
 * Filling in entry table takes constant time.
 * So the total time complexity = O(m⋅n).


 * Space complexity
 * Since we only used the  table on the heap, the total space complexity = O(m⋅n).