博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode Delete Operation for Two Strings
阅读量:4654 次
发布时间:2019-06-09

本文共 3173 字,大约阅读时间需要 10 分钟。

原题链接在这里:

题目:

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: 2Explanation: 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 }

或者像直接计算需要最小的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 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/7724197.html

你可能感兴趣的文章
Flex【原创】移动设备相册图片浏览功能
查看>>
Nodejs on windows 10
查看>>
HDU1233--还是畅通工程(最小生成树)
查看>>
linux——实际工作中如何使用linux
查看>>
设置 jsp 表格相邻两行的颜色不一样
查看>>
性能指标分析
查看>>
Jenkins企业应用
查看>>
[POJ 1061]青蛙的约会
查看>>
使用SeaJS实现模块化JavaScript开发
查看>>
开源项目如何赚钱
查看>>
BZOJ 1260 [CQOI2007]涂色paint(区间DP)
查看>>
HDU 1005 Number Sequence
查看>>
7 种将字符串反转的 Java 方法
查看>>
Softmax函数
查看>>
【题解】贪婪大陆
查看>>
notepad ++ 编辑 powershell profile 文件时的诡异问题
查看>>
这是一个测试文章
查看>>
struts2——文件下载(简单的功能)
查看>>
Java Bean Validation 最佳实践
查看>>
2018的第二篇博客 关于SSH框架的注解版整合
查看>>