Given two words word1 and word2, find the minimum number of operations required to convert word1 to 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')

 class Solution:

     def minDistance(self, x, y):
"""
:type word1: str
:type word2: str
:rtype: int
"""
m = len(x)
n = len(y)
dp = [[' '] * (n + 1) for i in range(m + 1)] for i in range(m + 1):
dp[i][0] = i
for j in range(n + 1):
dp[0][j] = j for i in range(1, m + 1):
for j in range(1, n + 1):
if x[i - 1] == y[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = 1 + min(dp[i - 1][j], dp[i]
[j - 1], dp[i - 1][j - 1])
return dp[m][n]

72. Edit Distance(编辑距离 动态规划)的更多相关文章

  1. [LeetCode] 72. Edit Distance 编辑距离

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

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

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  3. 【LeetCode】72. Edit Distance 编辑距离(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...

  4. 【Leetcode】72 Edit Distance

    72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...

  5. 刷题72. Edit Distance

    一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...

  6. [LeetCode] 72. Edit Distance(最短编辑距离)

    传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...

  7. [LeetCode] Edit Distance 编辑距离

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

  8. Edit Distance编辑距离(NM tag)- sam/bam格式解读进阶

    sam格式很精炼,几乎包含了比对的所有信息,我们平常用到的信息很少,但特殊情况下,我们会用到一些较为生僻的信息,关于这些信息sam官方文档的介绍比较精简,直接看估计很难看懂. 今天要介绍的是如何通过b ...

  9. 72. Edit Distance

    题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...

随机推荐

  1. linux命令之用户和用户组

    知识点: 1.-rwx--x--x (711) 只有所有者才有读,写,执行的权限,组群和其他人只有执行的权限 2.将root用户添加到supergroup用户组 groupadd supergroup ...

  2. 硬件日志:/var/log/dmesg

    在 Linux 系统启动时,会在屏幕上显示许多与硬件有关的信息,这些信息记录在 /var/log/dmesg 文件中,也可以用 dmesg 命令来查看 [root@localhost]# head / ...

  3. NUC970烧录文件系统

    燒錄U-Boot依照下列步驟將編譯完成的U-Boot燒錄至NAND Flash/SPI Flash/eMMC 中.U-Boot的編譯方法請參考4.3章節.3.11.1 燒錄所需檔案4. u-boot. ...

  4. Kconfig和Makefile的修改

    Kconfig文件的作用 内核源码树的目录下都有两个文件Kconfig(2.4版本是Config.in)和Makefile.分布到各目录的Kconfig构成了一个分布式的内核配置数据库,每个Kconf ...

  5. 【go】用Golang的 http 包建立 Web 服务器

    web.go package main import ( "fmt" "log" "net/http" "strings" ...

  6. mysql 效率 inner join 与 where in

    -- report 123.77k行 report_loss 620 行 -- inner join ;report_loss 索引 all report 索引 eq_ref ; -- total 0 ...

  7. EUI组件之TextInput

    输入文本,被废弃的组件,可以用EditableText代替

  8. JavaScript—文字自动变化为自定义颜色

    效果: JS代码: var ColorTimer; var Colorforn = 0; //颜色代码 var ColorArray = new Array("#00CCCC", ...

  9. 把 Activity 改成 ListActivity继续使用 setContentView

    ListActivity has a default layout that consists of a single, full-screen list in the center of the s ...

  10. centos6安装postgresql-(2)

    1.Install yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-ce ...