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. JDK 11中的ZGC-一种可扩展的低延迟垃圾收集器

    # 背景正如我们所知道的在JDK 11中即将迎来ZGC(The Z Garbage Collector),这是一个处于实验阶段的,可扩展的低延迟垃圾回收器.本文整合了外网几篇介绍ZGC的文章和代码. ...

  2. python3 基础语法(二)

    一.python3的基本数据类型: 和其他语言一样都包含了以下数据类型: 类型 含义 实例 INT 整型(integer) 1 FLOAT 浮点型 1.1 BOOL 布尔值 TRUE/FALSE ST ...

  3. SSL双向认证和SSL单向认证的流程和区别

    refs: SSL双向认证和SSL单向认证的区别https://www.jianshu.com/p/fb5fe0165ef2 图解 https 单向认证和双向认证!https://cloud.tenc ...

  4. kettle使用文件导入到Postgresql出现如下几种问题的总结

    1.kettle使用文件导入到Postgresql出现如下几种问题的总结: kettle使用文件导入到Postgresql出现如下几种问题的总结: .第一种错误,报错如ERROR: extra dat ...

  5. Spring Boot+CXF搭建WebService(转)

    概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...

  6. bat路径中有空格

    例如bat文件中写 C:/Program Files (x86)/Google/Chrome/Application/chrome.exe ./html/index.html pause   会报错, ...

  7. PBRT笔记(2)——BVH

    BVH 构建BVH树分三步: 计算每个图元的边界信息并且存储在数组中 使用指定的方法构建树 优化树,使得树更加紧凑 //BVH边界信息,存储了图元号,包围盒以及中心点 struct BVHPrimit ...

  8. Java 并发编程:线程间的协作(wait/notify/sleep/yield/join)

    Java并发编程系列: Java 并发编程:核心理论 Java并发编程:Synchronized及其实现原理 Java并发编程:Synchronized底层优化(轻量级锁.偏向锁) Java 并发编程 ...

  9. Resource Allocation of Yarn

    关键词:yarn 资源分配 mapreduce spark 简要指南 适合不想看太多原理细节直接上手用的人. 基本原则: container分配的内存不等于机器实际用掉的内存.NM给container ...

  10. 分布式mongodb分片集群

    本博客先简单介绍mongodb入门以及单实例以及mongodb的主从(主从官网是不提倡用的,原因后续介绍),副本集,分片. 第一:nosql介绍: 数据库分为关系型数据库与非关系型数据库,及具代表性的 ...