给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 。

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

插入一个字符
删除一个字符
替换一个字符
示例 1:

输入: word1 = "horse", word2 = "ros"
输出: 3
解释:
horse -> rorse (将 'h' 替换为 'r')
rorse -> rose (删除 'r')
rose -> ros (删除 'e')
示例 2:

输入: word1 = "intention", word2 = "execution"
输出: 5
解释:
intention -> inention (删除 't')
inention -> enention (将 'i' 替换为 'e')
enention -> exention (将 'n' 替换为 'x')
exention -> exection (将 'n' 替换为 'c')
exection -> execution (插入 'u')

 class Solution:
def minDistance(self, string1: str, string2: str) -> int:
m = len(string1)
n = len(string2)
f = [[None for _ in range(n+1)] for i in range(m+1)]
for i in range (m+1):
for j in range(n+1):
if i== 0:
f[i][j]=j
continue
if j == 0:
f[i][j] = i
continue
#insert delete replace
if string1[i-1] == string2[j-1]:
f[i][j]=f[i-1][j-1]
else:
f[i][j] = min(f[i-1][j],f[i][j-1],f[i-1][j-1]) + 1
return f[-1][-1]

参考 b站

LeetCode--072--编辑距离(python)的更多相关文章

  1. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  2. [LeetCode]题解(python):120 Triangle

    题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...

  3. [LeetCode]题解(python):119 Pascal's Triangle II

    题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...

  4. [LeetCode]题解(python):118 Pascal's Triangle

    题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...

  5. [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node

    题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...

  6. [LeetCode]题解(python):114 Flatten Binary Tree to Linked List

    题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...

  7. [LeetCode]题解(python):113 Path Sum II

    题目来源 https://leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf ...

  8. [LeetCode]题解(python):112 Path Sum

    题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...

  9. [LeetCode]题解(python):111 Minimum Depth of Binary Tree

    题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...

  10. [LeetCode]题解(python):110 Balanced Binary Tree

    题目来源 https://leetcode.com/problems/balanced-binary-tree/ Given a binary tree, determine if it is hei ...

随机推荐

  1. Java连接Hive使用Zookeeper的方式

    Java连接Hive的方式就是通过JDBC的方式来连接,URL为jdbc:hive2://host:port/db;principal=X@BIGDATA.COM等,这种方式是直接连接HiveServ ...

  2. sql语句实现行转列的3种方法实例

    sql语句实现行转列的3种方法实例 一般在做数据统计的时候会用到行转列,假如要统计学生的成绩,数据库里查询出来的会是这样的,但这并不能达到想要的效果,所以要在查询的时候做一下处理,下面话不多说了,来一 ...

  3. PHP 数组函数 内部指针

    current( &$arr ) 每个数组的当前单元,初始值的 数组的第一个单元next ( &$arr ) 返回数组中的下一个单元 , 如果没值则返回falshprev ( & ...

  4. 【ABAP系列】SAP ABAP中ALV使用HTML的例子

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP中ALV使用HT ...

  5. python+selenium模拟键盘输入

    from selenium.webdriver.common.keys import Keys #键盘导入类 --------------------------------------------- ...

  6. qt QUndoGroup的使用

    最近项目中用到撤销,恢复功能.qt的demo中有一个例子,是类似于单文档的.而我的项目中是类似于多文档的项目,即可能要打开多个页面,不同的页面都有撤销恢复功能.这样的话,就要用到QUndoGroup类 ...

  7. C++ 统计输入的句子有多少英文字母

    // ConsoleApplication1.cpp: 定义控制台应用程序的入口点.//#include "stdafx.h"#include <iostream>#i ...

  8. 1、Java语言概述与开发环境——编译和运行第一个程序HelloWorld.java

    编写一个Java程序到运行的步骤概述: 1.将Java代码编写到扩展名为.Java的文件中 2.通过Javac命令对该Java文件进行编译 3.通过Java命令对生成的class文件进行运行 一.编写 ...

  9. 通过编写串口助手工具学习MFC过程——(八)遇到的一些问题

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...

  10. go & flag

    参考 Golang下的flag模块使用 Go基础篇[第6篇]: 内置库模块 flag