码迷,mamicode.com
首页 > 其他好文 > 详细

Baozi Leetcode solution 72. Edit Distance

时间:2019-12-29 11:21:04      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:jee   new   cannot   detail   amp   ros   with   skin   current   

Problem Statement 

Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.

You have the following 3 operations permitted on a word:

  1. Insert a character
  2. Delete a character
  3. 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‘)

 Problem link

Video Tutorial

You can find the detailed video tutorial here

Thought Process

Brute force sounds like really overkill because we have to list all the possible ways and it is exponential time complexity. On a high level, the recursion looks like

  • if cannot convert A to B, return -1 (the recursion termination function); if A == B, then return 0
  • try 3 different ways, insert, remove and replace, cut that character and continue the recursion. Compare the minimum of that 3 methods (exclude -1)

Edit distance is a classic Dynamic Programming (DP) problem, just like Coin Change Problem. It follows the DP template perfectly (asking for extreme values)

 

The mathematical induction function is

DP[i][j] is the minimum operations needed to convert String[0-i] to String[0-j]

Initial values: dp[0][j] = j and dp[i][0] = i

If (A[i] == B[j]) dp[i][j] =  dp[i-1][j-1]  

Else min(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]) +1;

Solutions 

DP

 1 public int minDistance(String word1, String word2) {
 2     if (word1 == null || word2 == null) return -1;
 3 
 4     int l1 = word1.length();
 5     int l2 = word2.length();
 6 
 7     if (l1 == 0 || l2 == 0) {
 8         return l1 == 0 ? l2 : l1;
 9     }
10 
11     // A pattern for coding up DP, use extra array
12     // This could be further optimized into 2 arrays
13     int[][] lookup = new int[l1 + 1][l2 + 1];
14 
15     // Note the initialization case here, for an empty string, it will take i to change with the current one
16     // initialize
17     for (int i = 0; i <= l2; i++) {
18         lookup[0][i] = i;
19     }
20 
21     for (int i = 0; i <= l1;i++) {
22         lookup[i][0] = i;
23     }
24 
25     /* mathematical induction function:
26      * dp[i][j] =  dp[i-1][j-1]   if (A[i] == B[j])
27        or = min(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]) +1;
28         dp[0][j] = j and dp[i][0] = i   */
29     // this is O(M*N) time and M*N space, space could be saved using 2 arrays
30     for (int i = 1; i <= l1; i++) {
31         for (int j = 1; j <= l2; j++) {
32             if (word1.charAt(i-1) == word2.charAt(j-1)) {
33                 lookup[i][j] = lookup[i-1][j-1];
34             } else {
35                 lookup[i][j] = Math.min(lookup[i-1][j], Math.min(lookup[i][j-1], lookup[i-1][j-1])) + 1;
36             }
37         }
38     }
39 
40     return lookup[l1][l2];
41 }

 

Time Complexity: O(M*N) where M is word1 length and N is word2 length

Space Complexity: O(M*N) since we need an extra 2D array

 

References

Baozi Leetcode solution 72. Edit Distance

标签:jee   new   cannot   detail   amp   ros   with   skin   current   

原文地址:https://www.cnblogs.com/baozitraining/p/12114034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!