给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符。
示例:
输入: "sea", "eat"
输出: 2
解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea"
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/delete-operation-for-two-strings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public int minDistance(String word1, String word2) {
int m = word1.length(),n = word2.length();
//dp用于记录word1[0:i]与word2[0:j]需要删除删除的个数
int [][] dp = new int [m+1][n+1];
//将边界进行初始化
for(int i=1;i<=m;i++){
dp[i][0] = i;
}
for(int j=1;j<=n;j++){
dp[0][j] = j;
}
for(int i =1;i



