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 = 0 if word1[i] == word2[j] else 1
最后返回ans[m,n]即可. 这个模式跟[LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming
很像, 都是用上, 左和左上角的元素去递推现在的元素. 当然也可以用滚动数组的方式去将space 降为 O(n) 1. Constraints
1) size >= [0*0]
2) element can be anything, captal sensitive 2. ideas
Dynamic programming, T: O(m*n) S: O(m*n) => O(n) using rolling array 3. Codes
1) S: O(m*n)
 class Solution:
def editDistance(self, word1, word2):
m, n = len(word1), len(word2)
ans = [[0]*n+1 for _ in range(m+1)]
for i in range(1, m+1):
ans[i][0] = i
for j in range(1, n+1):
ans[0][j] = j
for i in range(1, m+1):
for j in range(1, n+1):
temp = 0 if word1[i-1] == word2[j-1] else 1
ans[i][j] = min(ans[i-1][j] + 1, ans[i][j-1] + 1, ans[i-1][j-1] + temp)
return ans[m][n]

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

 class Solution:
def editDistance(self, word1, word2):
m, n = len(word1), len(word2)
ans = [[0]*(n+1) for _ in range(2)]
for j in range(1, n+1):
ans[0][j] = j
ans[1][0] = 1
for i in range(1, m+1):
for j in range(1, n+1):
ans[i%2][0] = i
temp = 0 if word1[i-1] == word2[j-1] else 1
ans[i%2][j] = min(ans[i%2-1][j] + 1, ans[i%2][j-1] + 1, ans[i%2-1][j-1] + temp)
return ans[m%2][n]

4. Test cases

1)  "horse", "ros"


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

  1. [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  2. [LeetCode] 120. Triangle _Medium tag: Dynamic Programming

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  3. [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 ...

  4. [LeetCode] 788. Rotated Digits_Easy tag: **Dynamic Programming

    基本思路建一个helper function, 然后从1-N依次判断是否为good number, 注意判断条件为没有3,4,7 的数字,并且至少有一个2,5,6,9, 否则的话数字就一样了, 比如8 ...

  5. [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 gr ...

  6. [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  7. [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  8. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  9. [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

随机推荐

  1. 【Spring Boot&&Spring Cloud系列】提高数据库访问性能

    前言 使用关系型数据库的应用系统的性能瓶颈最终还是数据库.随着业务的迅速增长,数据量会不断增大,会逐渐暴露关系型数据库的弱点,性能会大幅度的降低 项目地址:https://github.com/And ...

  2. Tomorrow Is A New Day

    Sometimes we do not feel like we want to feel     Sometimes we do not achieve what we want to achiev ...

  3. UI设计中的高保真和低保真

    低保真一般用Axure Rp产出,高保真分两种,带交互的或不带交互的.不带交互的高保真直接根据低保真用PS产出即可.带交互的,需要 PS产出后,再切图,再使用Axure RP与低保真结合产出高保真. ...

  4. win8/win7中使用Git Extensions PuTTy模式提交时 git-credential-winstore.exe": No such file or directory 错误解决方案

    参考:http://www.cnblogs.com/hlizard/p/3627792.html 报错类似以下错误 \"F:/GitExtensions/GitCredentialWinSt ...

  5. Linux mint 亮度调节

    刚装上的mint亮度严重影响操作,快速调节mint亮度的方法 echo 1000 >/sys/class/backlight/intel_backlight/brightness 1000这个数 ...

  6. UVA 1335 Beijing Guards(二分答案)

    入口: https://cn.vjudge.net/problem/UVA-1335 [题意] 有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个 ...

  7. locate命令的使用

    使用locate命令,遇到了这样的情况:当前目录下有一个文件,而使用这个命令时却查找不到这个文件,上网查了一下,找到了原因,就在下面. 1. find find是最常见和最强大的查找命令,你可以用它找 ...

  8. 浅析重定向与反弹Shell命令

    0×01    简介 反弹shell在漏洞证明和利用的过程中都是一个直接有力的手段.由于安全工作或者学习的需要,我们或多或少都会接触到各种反弹shell的命令,于是就有了这个能稍微帮助初学者理解的文档 ...

  9. iOS - 引用计数探讨

    <Objective-C 高级编程> 这本书有三个章节,我针对每一章节进行总结并加上适当的扩展分享给大家.可以从下面这张图来看一下这三篇的整体结构: 注意,这个结构并不和书中的结构一致,而 ...

  10. webconfig的配置说明

    转自 :http://www.cnblogs.com/kissdodog/archive/2013/04/16/3025315.html <?xml version="1.0" ...