[LeetCode] 72. Edit Distance_hard tag: Dynamic Programming

Given two words word1 and word2, find the minimum number of operations required to convert word1to word2.

You have the following 3 operations permitted on a word:

  1. Insert a character
  2. Delete a character
  3. Replace a character

Example 1:

Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace ‘h‘ with ‘r‘)
rorse -> rose (remove ‘r‘)
rose -> ros (remove ‘e‘)

Example 2:

Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove ‘t‘)
inention -> enention (replace ‘i‘ with ‘e‘)
enention -> exention (replace ‘n‘ with ‘x‘)
exention -> exection (replace ‘n‘ with ‘c‘)
exection -> execution (insert ‘u‘)

这个题目思路是用Dynamic Programming, ans[i][j] 表明前i-1 个word1的字符与到前j-1个word2的字符的最小值, 然后ans[i][j] = min(ans[i-1][j]+ 1, ans[i][j-1] + 1, ans[i-1][j-1] + temp)其中temp = 1 if word1[i] == word2[j] else 0最后返回ans[m,n]即可. 这个模式跟[LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming很像, 都是用上, 左和左上角的元素去递推现在的元素. 当然也可以用滚动数组的方式去将space 降为 O(n)

1. Constraints1) size >= [0*0] 2) element can be anything, captal sensitive

2. ideasDynamic programming,   T: O(m*n)     S: O(m*n)  => O(n) using rolling array

3. Codes1) S: O(m*n)   
 1 class Solution:
 2     def editDistance(self, word1, word2):
 3         m, n = len(word1), len(word2)
 4         ans = [[0]*n+1 for _ in range(m+1)]
 5         for i in range(1, m+1):
 6             ans[i][0] = i
 7         for j in range(1, n+1):
 8             ans[0][j] = j
 9         for i in range(1, m+1):
10             for j in range(1, n+1):
11                 temp = 1 if word1[i] == word2[j] else 0
12                 ans[i][j] = min(ans[i-1][j] + 1, ans[i][j-1] + 1, ans[i-1][j-1] + temp)
13         return ans[m][n]

2) S: O(n) using 滚动数组

 1 class Solution:
 2     def editDistance(self, word1, word2):
 3         m, n = len(word1), len(word2)
 4         ans = [[0]*(n+1) for _ in range(2)]
 5         for j in range(1, n+1):
 6             ans[0][j] = j
 7         ans[1][0] = 1
 8         for i in range(1, m+1):
 9             for j in range(1, n+1):
10                 ans[i%2][0] = i
11                 temp = 0 if word1[i] == word2[j] else 1
12                 ans[i%2][j] = min(ans[i%2-1][j] + 1, ans[i%2][j-1] + 1, ans[i%2-1][j-1] + temp)
13         return ans[m%2][n]

4. Test cases

1)  "horse", "ros"



原文地址:https://www.cnblogs.com/Johnsonxiong/p/9339160.html

时间: 2024-08-13 16:15:46

[LeetCode] 72. Edit Distance_hard tag: Dynamic Programming的相关文章

[LeetCode] 256. Paint House_Easy tag: Dynamic Programming

There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the sam

[LeetCode] 276. Paint Fence_Easy tag: Dynamic Programming

There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the total number of ways you can paint the fence. Note:n and

[LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" Output: true Example 2: Input: s1 = "aabcc", s2 = "dbbca", s3 =

[LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈

Description There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more coins left. The player who take the coins with the most value wins. Could you please decide the first

Minimum Edit Distance with Dynamic Programming

1. Question / 实践题目 2. Analysis / 问题描述 3. Algorithm / 算法描述 3.1. Substitution 3.2. Insertion 3.3. Deletion 3.4. Sepcial Cases 3.5. Equation 4. Fill the table / 填表 4.1. Dimention 4.2. Range 4.3. Order 4.4. Related Code 5. Show Me the Code / 完整代码 6. T(n)

LeetCode 72. Edit Distance Java

72.Edit Distance(编辑距离) 题目: 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 思路: 多次选择试图得到最优解,那么考虑动态规划. 先假设word1有len1位,word2有len2位,建立数组step,step[i][j]就代表我们要将word1前 i 位转换为word2前 j 位的最少数量. 此时word1查找到第 i+1 位字母a,

[LeetCode] 724. Find Pivot Index_Easy tag: Dynamic Programming

Given an array of integers nums, write a method that returns the "pivot" index of this array. We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the ind

leetCode 72.Edit Distance (编辑距离) 解题思路和方法

Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a char

LeetCode 72 Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c) Repla