[leetcode-583-Delete Operation for Two Strings]

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:
The length of given words won‘t exceed 500.
Characters in given words can only be lower-case letters.

思路:

首先求最长公共子序列(LCS),然后,用两个字符串的长度分别减去公共子序列的长度,然后再相加即为要删除的长度。

最长公共子序列是经典的动态规划问题。

最长公共子序列问题存在最优子结构:这个问题可以分解成更小,更简单的“子问题”,这个子问题可以分成更多的子问题,因此整个问题就变得简单了。最长公共子序列问题的子问题的解是可以重复使用的,也就是说,更高级别的子问题通常会重用低级子问题的解。拥有这个两个属性的问题可以使用动态规划算法来解决,这样子问题的解就可以被储存起来,而不用重复计算。这个过程需要在一个表中储存同一级别的子问题的解,因此这个解可以被更高级的子问题使用。

动态规划的一个计算最长公共子序列的方法如下,以两个序列{\displaystyle X}、{\displaystyle Y}为例子:

设有二维数组表示位和位之前的最长公共子序列的长度,则有:

其中,的第位与的第位完全相同时为“1”,否则为“0”。

此时,中最大的数便是的最长公共子序列的长度,依据该数组回溯,便可找出最长公共子序列。

 int minDistance2(string word1, string word2)
     {
         vector<vector<int>> dp(word1.size()+1,vector<int>(word2.size()+1,0));
         for (int i = 0; i <= word1.size();i++)
         {
             for (int j = 0; j <= word2.size();j++)
             {
                 if (i == 0 || j == 0) dp[i][j] = 0;
                 else if (word1[i-1] == word2[j-1])
                 {
                     dp[i][j] = dp[i - 1][j - 1] + 1;
                 }
                 else
                 {
                     dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
                 }

             }
         }
         int lcs = dp[word1.size()][word2.size()];
         return word1.size() - lcs + word2.size() - lcs;
     }

参考:

http://www.geeksforgeeks.org/dynamic-programming-set-4-longest-common-subsequence/

https://zh.wikipedia.org/wiki/%E6%9C%80%E9%95%BF%E5%85%AC%E5%85%B1%E5%AD%90%E5%BA%8F%E5%88%97

https://discuss.leetcode.com/topic/89285/java-dp-solution-longest-common-subsequence

时间: 2024-11-07 00:32:36

[leetcode-583-Delete Operation for Two Strings]的相关文章

[LeetCode] 583. Delete Operation for Two Strings 两个字符串的删除操作

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 ne

583. Delete Operation for Two Strings

Problem statement: 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

[LeetCode] Delete Operation for Two Strings 两个字符串的删除操作

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 ne

[Swift]LeetCode583. 两个字符串的删除操作 | Delete Operation for Two Strings

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 ne

LeetCode 712. Minimum ASCII Delete Sum for Two Strings

原题链接在这里:https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/ 题目: Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. Example 1: Input: s1 = "sea", s2 = "eat" Output: 2

【leetcode刷题笔记】Multiply Strings

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 题解:就是让实现一个大整数乘法. 假设两个数num1和num2的长度分别是len1和len2,那么最后得到的答案,在最高位有进位的时候,就是len1+len2位,否则是len1+len2

【leetcode】893. Groups of Special-Equivalent Strings

Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-of-special-equivalent-strings/ 1)problem You are given an array A of strings. Two strings S and T are special-equivalent if after any number of moves,

LeetCode Insert Delete GetRandom O(1)

原题链接在这里:https://leetcode.com/problems/insert-delete-getrandom-o1/?tab=Description 题目: Design a data structure that supports all following operations in average O(1) time. insert(val): Inserts an item val to the set if not already present. remove(val)

[LeetCode][JavaScript]Delete Node in a Linked List

Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list

[LeetCode][SQL]Delete Duplicate Emails

https://leetcode.com/problems/delete-duplicate-emails/ Delete Duplicate Emails Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. +----+------------------+ | Id | Emai