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') 思路

   这道题是一道典型的使用动态规划来解决的题目。两个单词我们申请一个(m+1)*(n+1)的矩阵,首先对矩阵的第一行和第一列进行初始化,然后从第二行第二个位置开始进行遍历,每次得到最小的编辑数。 这里如果当前两个字母相等的话,直接使其等于上一个字母的编辑数,也即dp[i][j] = dp[i-1][j-1]。但是当两个字母不相等的时候,我们可以从左边上边和右上角选出最小的编辑数在加一,得到当前位置的编辑数,也即dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1]))+1。这样直到循环遍历到矩阵的末尾。最后一个数字也即是最小编辑距离。时间复杂度为O(m*n),空间复杂度为O(m*n)。
  一般对于动态规划来题目来说,我们除了设置一个(m+1)*(n+1)的矩阵外,还可以使用(n+1)大小的矩阵。这里动态方程还是一样的,只不过这里我们需要处理的细节更多一些。时间复杂度和上面的一样,空间复杂度为O(n+1)。
图示步骤

    解决代码
  第一种空间复杂度为O(m*n)的解法
 class Solution(object):
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
if not word1 or not word2: # 一个为空直接返回另一个不为空的长度。
return len(word1) if not word2 else len(word2) m, n= len(word1), len(word2)
dp = []
for i in range(m+1): # 构造辅助矩阵
dp.append([0]*(n+1)) for i in range(1, m+1): # 初始化第一列
dp[i][0] = i for j in range(1, n+1): # 初始化第一行
dp[0][j] = j for i in range(1, m+1): # 逐个求解
for j in range(1, n+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], min(dp[i-1][j], dp[i][j-1]))+1
return dp[m][n]
  空间复杂度为O(n)的解法
 class Solution(object):
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
if not word1 or not word2:
return len(word1) if not word2 else len(word2)
m, n= len(word1), len(word2)
dp = [0]*(n+1) # 申请辅助数据
for i in range(1, n+1): # 初始化第一行
dp[i] = i for i in range(1,m+1): # 循环遍历
pre = dp[0] # 记录下dp[0]的值,也即为上面矩阵中dp[i-1][j-1]的值。
dp[0]= i # 给dp[0]赋值为当前单词编辑列的距离,也就是上面的初始化第一列
for j in range(1, n+1):
tem = dp[j] # 相当于记录下dp[i][j-1]的值,
if word1[i-1] == word2[j-1]: # 单词相等的时候
dp[j] = pre
else:
dp[j] = min(pre, min(dp[j-1], dp[j]))+1
pre = tem # 更新值 return dp[-1]

												

【LeetCode每天一题】Edit Distance(编辑距离)的更多相关文章

  1. [LeetCode] Edit Distance 编辑距离

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

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

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

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

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

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

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

  5. 【LeetCode】161. One Edit Distance

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...

  6. LeetCode解题报告—— N-Queens && Edit Distance

    1. N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no ...

  7. LeetCode(72) Edit Distance

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

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

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

  9. Java解决LeetCode72题 Edit Distance

    题目描述 地址 : https://leetcode.com/problems/edit-distance/description/ 思路 使用dp[i][j]用来表示word1的0~i-1.word ...

随机推荐

  1. tensorflow会话控制-【老鱼学tensorflow】

    在tensorflow中,当定义好结构后,就要通过tf.session()来建立运行时的会话. 本例子应该不难理解,我们用tensorflow来计算一下一个1行2列的矩阵和2行1列矩阵的乘积: imp ...

  2. SQL Server 中执行Shell脚本计算本地文件的内容大小

    SQL Server 数据库中除了能执行基本的SQL语句外,也可以执行Shell脚本.默认安装后,SQL中的Shell脚本的功能是关闭的,需要手动打开, 执行以下脚本即可打开该功能. -- 允许配置高 ...

  3. Centos下安装配置Mongodb3.6

    首先更新系统 yum -y update 1.安装Mongodb 编辑Mongodb安装源 vim /etc/yum.repos.d/mongodb-org-3.6.repo 编辑内容如下: [mon ...

  4. linux抓包工具tcpdump使用总结

    tcpdump采用命令行方式对接口的数据包进行筛选抓取,其丰富特性表现在灵活的表达式上 1.格式 # tcpdump --help tcpdump version 4.1-PRE-CVS_2012_0 ...

  5. 2019-2-25SqlServer 中所有表、列、视图、索引、主键、外键等常用sql

    sp_help Accounts_Users     其中Accounts_Users 表示表名 sp_columns Accounts_Users exec  sp_helpconstraint   ...

  6. VB进行RGB分色

    Option Explicit Private Type RGBA R As Byte G As Byte B As Byte A As Byte End Type Private Declare S ...

  7. Web 录音

    所需文件下载地址 链接:https://pan.baidu.com/s/1Dzbv8gPUZJ3T8Fe02hOJvg 提取码:zbgt py文件 from flask import Flask, j ...

  8. CMake入门

    CMake入门 CMake是一个跨平台的安装编译工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似 ...

  9. php的imagick模块扩展

    imagick模块介绍       ImageMagick是一个用于查看.编辑位图文件以及进行图像格式转换的开放源代码软件套装.它可以读取.编辑超过100种图象格式,可用来替换GD库. 安装 在加载模 ...

  10. java代码生成Excel文件3000条自定义属性的的域账户名

    一个项目为了测试需要模拟3000条域用户,将数据保存在Excel表格,然后导入到与服务器里. 我们今天要做的是自动生成3000条数据,并将这些数据保存在excel表格里面. 需要jar包:poi-3. ...