[leetCode]583. 两个字符串的删除操作

题目

链接:https://leetcode-cn.com/problems/delete-operation-for-two-strings

给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符。
示例:

输入: “sea”, “eat”
输出: 2
解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea"

提示:

  • 给定单词的长度不超过500。
  • 给定单词中的字符只含有小写字母。

动态规化

此题可以转化为求最大公共子序列的问题,求出最大公共子序列的长度n后用两个字符串长度之和减去2*n就是需要删除的字符数。
注意:题目中的序列是可以不连续的。

class Solution {
    public int minDistance(String word1, String word2) {
        int len1 = word1.length(), len2 = word2.length();
        int[][] dp = new int[len1 + 1][len2 + 1];
        for (int i = 1; i <= len1; i++) {
            for (int j = 1; j <= len2; j++) {
                if (word1.charAt(i-1) == word2.charAt(j-1)) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        return len1 + len2 - 2 * dp[len1][len2];
    } 
}

你可能感兴趣的:(LeetCode,#,动态规化)