day56|● 583. 两个字符串的删除操作 ● 72. 编辑距离

chatgpt/2023/10/4 8:25:23

● 583. 两个字符串的删除操作

Given two strings word1 and word2, return the minimum number of steps required to make word1 and word2 the same. can delete in either string.
Input: word1 = “sea”, word2 = “eat”
Output: 2
Explanation: You need one step to make “sea” to “ea” and another step to make “eat” to “ea”.

这题可以同时在两个字符串进行删除操作,问最少的操作次数( Math.min )。

如果word1[i-1] = word2[j-1],那么就不用进行删除,只用看前面需要多少次删除。
如果不等,就表示需要删除,可以删除word1或者word2,如果同时删除word1[i-1] and word2[j-1],步数需要+2,但其实也包含在了dp[i-1][j]+1中,即可以在[0:j]中再删除word2[j-1]

class Solution {public int minDistance(String word1, String word2) {int[][] dp = new int[word1.length()+1][word2.length()+1];for (int i = 0; i <= word1.length(); i++) {dp[i][0] = i;}for (int j = 0; j <= word2.length(); j++) {dp[0][j] = j;}for (int i = 1; i <= word1.length(); i++) {for (int j = 1; j <= word2.length(); j++) {if (word1.charAt(i-1) == word2.charAt(j-1)) {dp[i][j] = dp[i-1][j-1];} else {dp[i][j] = Math.min(dp[i-1][j]+1, dp[i][j-1]+1); // include dp[i-1][j-1]+2}}}return dp[word1.length()][word2.length()];}
}

● 72. 编辑距离

word1 -> word2
Insert a character
Delete a character
Replace a character
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’)

根据前面三题铺垫,就比较简单了。
word1[i-1] = word2[j-1],不用任何编辑,继续看word1[0: i-2]和word2[0: j-2]之间的操作数。
不相等,代表需要编辑操作:

  1. delete word1, word1删除一个元素,那么就是以下标i - 2为结尾的word1 与 j-1为结尾的word2的最近编辑距离 再加上一个操作。
  2. insert word1,也可以看做delete word2,比较的是word1[i]和word2[j-1],再加上一步操作。
  3. replace word1 with one, word1和word2都抛弃最后一位,再加上1。
class Solution {public int minDistance(String word1, String word2) {int[][] dp = new int[word1.length()+1][word2.length()+1];for (int i = 0; i <= word1.length(); i++) {dp[i][0] = i;}for (int j = 0; j <= word2.length(); j++) {dp[0][j] = j;}for (int i = 1; i <= word1.length(); i++) {for (int j = 1; j <= word2.length(); j++) {if (word1.charAt(i-1) == word2.charAt(j-1)) {dp[i][j] = dp[i-1][j-1];} else {// delete, insert, replacedp[i][j] = Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) +1;}}}return dp[word1.length()][word2.length()];}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.exyb.cn/news/show-5314375.html

如若内容造成侵权/违法违规/事实不符,请联系郑州代理记账网进行投诉反馈,一经查实,立即删除!

相关文章

设计原则学习之里氏替换原则

以下内容均来自抖音号【it楠老师教java】的设计模式课程。 1、原理概述 子类对象&#xff08;objectofsubtype/derivedclass&#xff09;能够替换程序&#xff08;program&#xff09;中父类对象&#xff08;objectofbase/parentclass&#xff09;出现的任何地方&#xff0c…

手写MyBatis代码生成器Maven插件

代码链接&#xff1a;codegen-maven-plugin 在使用MyBatis时&#xff0c;需要根据库表结构编写一些通用的Mapper interface、XML、Entity&#xff0c;这些重复操作可以通过代码生成器自动生成&#xff0c;大大提高开发效率。 目前&#xff0c;代码生成分为两种方式&#xff1a…

【flutter】flutter如何让app内字体大小不随着系统改变而改变

如果我们不特意设置&#xff0c;flutter开发的app他的字体大小是会跟着系统设置的字体大小而改变&#xff0c;这样就会导致页面出现布局错乱问题&#xff0c;那么如何解决这个问题呢&#xff1f;我也搜索了相关资料&#xff0c;有两个常用也是网络上搜集到比较多的方法&#xf…

中国计算机学会推荐国际学术期刊(网络与信息安全)

序号刊物简称刊物全称分类类型专业领域1TDSCIEEE Transactions on Dependable and Secure ComputingA期刊网络与信息安全2TIFSIEEE Transactions on Information Forensics and SecurityA期刊网络与信息安全3Journal of CryptologyA期刊网络与信息安全1TISSECACM Transactions …

通过SSH实现将本地端口反向代理到公网服务器

使用场景 有一台公网服务器&#xff0c;能够对外开放服务进行访问&#xff0c;但是这个公网服务器资源较低&#xff0c;无法运行太多服务 有一台闲置电脑可以全天候开机使用&#xff0c;且配置较好&#xff0c;可以部署多个服务&#xff0c;但是没有公网IP 需求&#xff1a;将…

时序预测 | Python实现NARX-DNN空气质量预测

时序预测 | Python实现NARX-DNN空气质量预测 目录 时序预测 | Python实现NARX-DNN空气质量预测效果一览基本介绍研究内容程序设计参考资料效果一览 基本介绍 时序预测 | Python实现NARX-DNN空气质量预测 研究内容 Python实现NARX-DNN空气质量预测,使用深度神经网络对比利时空气…

最新多模态3D目标检测论文汇总(PDF+代码)

目前在自动驾驶领域&#xff0c;多模态3D目标检测是一个非常重要的研究热点。由于引入了其他传感器数据&#xff0c;多模态3D目标检测在性能上明显优于纯视觉的方案&#xff0c;可以同时预测周围物体的类别、位置和大小&#xff0c;因此对于自动驾驶领域的同学来说&#xff0c;…

ifcfg-ens33中的ONBOOT字段是什么作用?

在CentOS或其他基于Red Hat Enterprise Linux (RHEL)的Linux发行版中&#xff0c;ifcfg-ens33是网络配置文件&#xff0c;用于配置网卡设备ens33的网络参数。ifcfg-ens33文件位于/etc/sysconfig/network-scripts/目录下&#xff08;可能因系统版本而略有不同&#xff0c;例如/e…
推荐文章