代码随想录算法训练营第五十六天| LeetCode583. 两个字符串的删除操作、LeetCode72. 编辑距离

一、LeetCode583. 两个字符串的删除操作

        1:题目描述(583. 两个字符串的删除操作)

        给定两个单词 word1 和 word2 ,返回使得 word1 和  word2 相同所需的最小步数

        每步 可以删除任意一个字符串中的一个字符。

代码随想录算法训练营第五十六天| LeetCode583. 两个字符串的删除操作、LeetCode72. 编辑距离_第1张图片

        2:解题思路

class Solution:
    def minDistance(self, word1: str, word2: str) -> int:
        # 确定dp数组的含义
        # dp[i][j]:以i-1结尾的字符串word1,和以j-1结尾的字符串word2,想要达到相等,所需要删除的元素最少次数为dp[i][j]
        # 递推公式:分为word1[i-1]==word2[j-1]和word1[i-1]!=word2[j-1]
        # word1[i-1]==word2[j-1]:不用删除元素,dp[i][j] = dp[i-1][j-1]
        # word1[i-1]!=word2[j-1]:分为删除word1[i-1],删除word2[j-1],删除word1[i-1]和word2[j-1]
        # 删除word1[i-1]:dp[i][j] = dp[i-1][j]+1
        # 删除word2[j-1]:dp[i][j] = dp[i][j-1]+1
        # 删除word1[i-1]和word2[j-1]:dp[i][j] = dp[i-1][j-1]+2
        # 即:dp[i][j] = min(dp[i-1][j]+1,dp[i][j-1]+1,dp[i][j] = dp[i-1][j-1]+2)
        # 初始化,需要初始化dp[i][0],dp[0][j]
        # dp[i][0]:word2为空字符串,以i-1结尾的字符串word1要删除多少个元素,才能与word2相等
        # dp[i][0] = i
        # dp[0][j]:word1为空字符串,以j-1结尾的字符串word2要删除多少个元素,才能与word1相等
        # dp[0][j] = j
        # 遍历顺序:从下标1开始,从上到下,从左到右遍历
        word1_len = len(word1)
        word2_len = len(word2)
        dp = [[0 for _ in range(word2_len+1)] for _ in range(word1_len+1)]
        # 初始化
        for i in range(word1_len+1):
            dp[i][0] = i
        for j in range(word2_len+1):
            dp[0][j] = j
        # 遍历
        for i in range(1,word1_len+1):
            for j in range(1,word2_len+1):
                if word1[i-1] == word2[j-1]:
                    dp[i][j] = dp[i-1][j-1]
                else:
                    dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+2)
        return dp[-1][-1]

二、LeetCode72. 编辑距离

        1:题目描述(72. 编辑距离)

        给你两个单词 word1 和 word2, 请返回将 word1 转换成 word2 所使用的最少操作数  。

        你可以对一个单词进行如下三种操作:

  • 插入一个字符
  • 删除一个字符
  • 替换一个字符

代码随想录算法训练营第五十六天| LeetCode583. 两个字符串的删除操作、LeetCode72. 编辑距离_第2张图片

        2:解题思路

class Solution:
    def minDistance(self, word1: str, word2: str) -> int:
        # 确认dp数组的含义
        # dp[i][i]表示以下标i-1结尾的字符串word1和以下标j-1结尾的字符串word2,最近编辑距离为dp[i][j]
        # 确认递推公式
        # 分为两个情况:word1[i-1]==word2[j-1]和word1[i-1]!=word2[j-1]
        # word1[i-1]==word2[j-1]:不用操作,dp[i][j] = dp[i-1][j-1]
        # word1[i-1]!=word2[j-1]:分为word1增加元素,删除元素,替换元素
        # word1增加一个元素,相当于word2删除一个元素:dp[i][j] = dp[i][j-1]+1
        # word1删除一个元素:dp[i][j] = dp[i-1][j]+1
        # word1替换一个元素:dp[i][j] = dp[i-1][j-1]+1
        # 即:dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])+1
        # 初始化:初始化dp[i][0], dp[0][j]
        # dp[i][0]:以下标i-1为结尾的字符串word1,和空字符串word2,最近编辑距离为dp[i][0]
        # 需要对word1中所有元素进行删除操作,dp[i][0] = i
        # dp[0][j]:以下标j-1为结尾的字符串word2,和空字符串word1,最近编辑距离为dp[0][j]
        # 需要对word1进行插入word2中的全部字符,dp[0][j] = j
        # 遍历顺序:从下标1开始,从上到下,从左到右进行遍历
        word1_len = len(word1)
        word2_len = len(word2)
        dp = [[0 for _ in range(word2_len+1)] for _ in range(word1_len+1)]
        for i in range(word1_len+1):
            dp[i][0] = i
        for j in range(word2_len+1):
            dp[0][j] = j
        for i in range(1, word1_len+1):
            for j in range(1, word2_len+1):
                if word1[i-1] == word2[j-1]:
                    dp[i][j] = dp[i-1][j-1]
                else:
                    dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])+1
        return dp[-1][-1]

你可能感兴趣的:(算法训练营(LeetCode),leetcode,算法,动态规划,python)