Delete Operation for Two Strings

原题: https://leetcode.com/problems/delete-operation-for-two-strings/description/

题意: 给定单词word1和word2,从word1和/或word2中删去一些字符,使得word1和word2相同,求最少删除的字符数。

约定:(1)单词长度不超过500;(2)单词只包含小写字母。

例子: 

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

标签: word1、word2、ea、sea、单词、面试
猜你感兴趣的圈子:
LeetCode交流圈
  • SLPH
    2017-08-13 12:54:40 1楼#1层
    Python解法一:最长公共子序列
    class Solution(object):
        def minDistance(self, word1, word2):
            """
            :type word1: str
            :type word2: str
            :rtype: int
            """
            return len(word1) + len(word2) - 2 * self.lcs(word1, word2)
    
        def lcs(self, word1, word2):
            len1, len2 = len(word1), len(word2)
            dp = [[0] * (len2 + 1) for x in range(len1 + 1)]
            for x in range(len1):
                for y in range(len2):
                    dp[x + 1][y + 1] = max(dp[x][y + 1], dp[x + 1][y])
                    if word1[x] == word2[y]:
                        dp[x + 1][y + 1] = dp[x][y] + 1
            return dp[len1][len2]
  • SLPH
    2017-08-13 12:55:44 2楼#1层
    Python解法二:动态规划
    class Solution(object):
        def minDistance(self, word1, word2):
            """
            :type word1: str
            :type word2: str
            :rtype: int
            """
            len1, len2 = len(word1), len(word2)
            dp = [[0] * (len2 + 1) for x in range(len1 + 1)]
            for x in range(len1 + 1):
                for y in range(len2 + 1):
                    if x == 0 or y == 0:
                        dp[x][y] = x + y
                    elif word1[x - 1] == word2[y - 1]:
                        dp[x][y] = dp[x - 1][y - 1]
                    else:
                        dp[x][y] = min(dp[x - 1][y], dp[x][y - 1]) + 1
            return dp[len1][len2]
  • SLPH
    2017-08-13 12:56:14 3楼#1层
    C++解法一:
    class Solution {
    public:
        int minDistance(string word1, string word2) {
            int n1 = word1.size(), n2 = word2.size();
            vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1, 0));
            for (int i = 1; i <= n1; ++i) {
                for (int j = 1; j <= n2; ++j) {
                    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]);
                    }
                }
            }
            return n1 + n2 - 2 * dp[n1][n2];
        }
    };
  • SLPH
    2017-08-13 12:56:39 4楼#1层
    C++解法二:
    class Solution {
    public:
        int minDistance(string word1, string word2) {
            int n1 = word1.size(), n2 = word2.size();
            vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1, 0));
            for (int i = 0; i <= n1; ++i) dp[i][0] = i;
            for (int j = 0; j <= n2; ++j) dp[0][j] = j;
            for (int i = 1; i <= n1; ++i) {
                for (int j = 1; j <= n2; ++j) {
                    if (word1[i - 1] == word2[j - 1]) {
                        dp[i][j] = dp[i - 1][j - 1];
                    } else {
                        dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1]);
                    }
                }
            }
            return dp[n1][n2];
        }
    };
  • SLPH
    2017-08-13 12:57:34 5楼#1层
    C++解法三:
    class Solution {
    public:
        int minDistance(string word1, string word2) {
            int n1 = word1.size(), n2 = word2.size();
            vector<vector<int>> memo(n1 + 1, vector<int>(n2 + 1, 0));
            return helper(word1, word2, 0, 0, memo);
        }
        int helper(string word1, string word2, int p1, int p2, vector<vector<int>>& memo) {
            if (memo[p1][p2] != 0) return memo[p1][p2];
            int n1 = word1.size(), n2 = word2.size();
            if (p1 == n1 || p2 == n2) return n1 - p1 + n2 - p2;
            if (word1[p1] == word2[p2]) {
                memo[p1][p2] = helper(word1, word2, p1 + 1, p2 + 1, memo);
            } else {
                memo[p1][p2] = 1 + min(helper(word1, word2, p1 + 1, p2, memo), helper(word1, word2, p1, p2 + 1, memo));
            }
            return memo[p1][p2];
        }
    };
  • 回复
隐藏