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. Vue CLI 3+tinymce 5富文本编辑器整合

    基于Vue CLI 3脚手架搭建的项目整合tinymce 5富文本编辑器,vue cli 2版本及tinymce 4版本参考:https://blog.csdn.net/liub37/article/ ...

  2. XMLHttpRequest.withCredentials 解决跨域请求头无Cookie的问题

    查看原文 XMLHttpRequest.withCredentials  属性是一个Boolean类型,它指示了是否该使用类似cookies,authorization headers(头部授权)或者 ...

  3. vmware ubuntu硬盘空间不够用,空间扩展

    我从来没有想过我的虚拟机内存会不够用,毕竟已经20G了,可是最近学习python,装了些学习有关的软件, 期末做libvirt管理实验,存了两个镜像,就变成这样了,所以,我就像了要扩展硬盘空间,在网上 ...

  4. net core体系-API-Restful+Swagger搭建API

    本篇主要简单介绍下.net core下搭建WebApi 项目结构 项目结构其实不用多说,基本上大同小异. Controller:对外暴露的契约 Business/IBussiness:业务逻辑层实现及 ...

  5. 2.基于梯度的攻击——FGSM

    FGSM原论文地址:https://arxiv.org/abs/1412.6572 1.FGSM的原理 FGSM的全称是Fast Gradient Sign Method(快速梯度下降法),在白盒环境 ...

  6. 【Linux】Linux简介

    思维导图 什么是Linux? Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统. Linux能运行主要的UNIX工 ...

  7. SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)

    在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...

  8. C语言面对对象设计模式汇编

    面向对象发展到今天,已经出现了许许多多优秀的实践.方法和技术.很多的技术都能够有效的提高软件质量.IBM上的<面向对象软件开发和过程>系列文章对面对对象设计从如下层面进行了详细的介绍:代码 ...

  9. 根据文件大小自动判断单位B,KB,MB,GB

    <php> /** * 文件大小格式化 * @param integer $size 初始文件大小,单位为byte * @return array 格式化后的文件大小和单位数组,单位为by ...

  10. Linux——模拟实现一个简单的shell(带重定向)

    进程的相关知识是操作系统一个重要的模块.在理解进程概念同时,还需了解如何控制进程.对于进程控制,通常分成1.进程创建  (fork函数) 2.进程等待(wait系列) 3.进程替换(exec系列) 4 ...