LeetCode 583. Delete Operation for Two Strings

原题链接在这里:https://leetcode.com/problems/delete-operation-for-two-strings/description/

题目:

Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.

Example 1:

Input: "sea", "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".

Note:

  1. The length of given words won't exceed 500.
  2. Characters in given words can only be lower-case letters. 

题解:

找LCS的长度n. word1.length()+word2.length()-2*n. 就是答案.

用DP找LCS的长度. 需要储存的历史信息是到当前点的LCS长度. 用dp[i][j]储存, 表示word1到i和word2到j的LCS长度.

递推时, 若是当前字符match, 在dp[i-1][j-1]的基础上加1即可.

若不match, 取dp[i][j-1] 和 dp[i-1][j]中较大值即可.

初始化都是0.

Time Complexity: O(m*n). m = word1.length(), n = word2.length().

Space: O(m*n).

AC Java:

 1 class Solution {
 2     public int minDistance(String word1, String word2) {
 3         int len1 = word1.length();
 4         int len2 = word2.length();
 5         int [][] dp = new int[len1+1][len2+1];
 6         for(int i = 0; i<=len1; i++){
 7             for(int j = 0; j<=len2; j++){
 8                 if(i==0 || j==0){
 9                     continue;
10                 }else if(word1.charAt(i-1) == word2.charAt(j-1)){
11                     dp[i][j] = 1 + dp[i-1][j-1];
12                 }else{
13                     dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
14                 }
15             }
16         }
17         return len1+len2-2*dp[len1][len2];
18     }
19 }

或者像Edit Distance直接计算需要最小的operations数目.

DP问题. 需要储存的历史信息是各自到当前的位置变成相同string需要的最小operation数目. 用二维数组来出巡.

递推时, 若是当前字符match, 不需要额外操作. dp[i][j] = dp[i-1][j-1].

若不match, 需要在dp[i-1][j], dp[i][j-1]中取较小值加1.

初始化或一边在初始位置没动, 最小operation数目就是另一边的位置全部减掉.

答案dp[m][n]. m = word1.length(). n = word2.length().

Time Complexity: O(m*n).

Space: O(m*n).

AC Java:

 1 class Solution {
 2     public int minDistance(String word1, String word2) {
 3         int m = word1.length();
 4         int n = word2.length();
 5         int [][] dp = new int[m+1][n+1];
 6         for(int i = 0; i<=m; i++){
 7             for(int j = 0; j<=n; j++){
 8                 if(i == 0 || j == 0){
 9                     dp[i][j] = i+j;
10                 }else if(word1.charAt(i-1) == word2.charAt(j-1)){
11                     dp[i][j] = dp[i-1][j-1];
12                 }else{
13                     dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + 1;
14                 }
15             }
16         }
17         return dp[m][n];
18     }
19 }

也可以降维节省空间.

Time Complexity: O(m*n).

Space: O(n).

AC Java:

 1 class Solution {
 2     public int minDistance(String word1, String word2) {
 3         int m = word1.length();
 4         int n = word2.length();
 5         int [] dp = new int[n+1];
 6         for(int i = 0; i<=m; i++){
 7             int [] temp = new int[n+1];
 8             for(int j = 0; j<=n; j++){
 9                 if(i == 0 || j == 0){
10                     temp[j] = i+j;
11                 }else if(word1.charAt(i-1) == word2.charAt(j-1)){
12                     temp[j] = dp[j-1];
13                 }else{
14                     temp[j] = Math.min(dp[j], temp[j-1]) + 1;
15                 }
16             }
17             dp = temp;
18         }
19         return dp[n];
20     }
21 }

类似Longest Common SubsequenceMinimum ASCII Delete Sum for Two StringsEdit Distance.

posted @ 2017-10-24 16:16  Dylan_Java_NYC  阅读(463)  评论(0编辑  收藏  举报