Leetcode 583.两个字符串的删除操作
两个字符串的删除操作
给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符。
示例 1:
输入: "sea", "eat"
输出: 2
解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea"
说明:
- 给定单词的长度不超过500。
- 给定单词中的字符只含有小写字母。
思路
首先求出最长公共子序列,然后字符串的长度减去最长公共子序列的长度就是要删除的长度。

c[i,j]表示:(x1,x2....xi) 和 (y1,y2...yj) 的最长公共子序列的长度。
class Solution {
public int minDistance(String word1, String word2) {
int n=word1.length();
int m=word2.length();
int[][] dp=new int[n+1][m+1];
for(int i=0;i<=n;i++) dp[i][0]=0;
for(int j=0;j<=m;j++) dp[0][j]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(word1.charAt(i-1)==word2.charAt(j-1)) dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
}
}
return word1.length()+word2.length()-2*dp[n][m];
}
}
Leetcode 583.两个字符串的删除操作的更多相关文章
- Java实现 LeetCode 583 两个字符串的删除操作(求最长公共子序列问题)
583. 两个字符串的删除操作 给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符. 示例: 输入: " ...
- [LeetCode] 583. Delete Operation for Two Strings 两个字符串的删除操作
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...
- [LeetCode] Delete Operation for Two Strings 两个字符串的删除操作
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...
- [Swift]LeetCode583. 两个字符串的删除操作 | Delete Operation for Two Strings
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...
- Java实现 LeetCode 712 两个字符串的最小ASCII删除和(最长公共子串&&ASCII值最小)
712. 两个字符串的最小ASCII删除和 给定两个字符串s1, s2,找到使两个字符串相等所需删除字符的ASCII值的最小和. 示例 1: 输入: s1 = "sea", s2 ...
- Leetcode 712. 两个字符串的最小ASCII删除和
题目描述: https://leetcode-cn.com/problems/minimum-ascii-delete-sum-for-two-strings/ 解题思路: 也是典型的dp问题.利用二 ...
- leetcode 415 两个字符串相加
string addstring(string s1,string s2) { string ans=""; ; ,j=s2.length()-;i>=||j>=;i- ...
- [LeetCode] 712. Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和
Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...
- [LeetCode] Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和
Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...
随机推荐
- 使用后台程序的第一个程序hello word
1.在advanced\backend\SiteController.php中输入 2.在advanced\backend\Views文件夹下添加名字为say.php的文件,文件名必须和控制器中的视图 ...
- openstack RuntimeError: Unable to create a new session key. It is likely that the cache
[Mon Apr 15 01:02:31.654247 2019] [:error] [pid 19433:tid 139790082479872] Login successful for user ...
- selenium显示等待解决浏览器未加载完成查找控件的问题
问题描述:wap版支付成功后,跳转到支付成功页,查找的元素已出现,如图的:元素1,元素2,但是提示查找的元素超时,失败,并且每到这个页面都会报页面超时,不能查找到页面元素 原始代码: try{ op. ...
- tableviewcell折叠问题,(类似qq列表展开形式) 多个cell同时展开,OC版 和 Swift
之前没有用到过这块,但是今天看到,就试了试,但是发现,网上的有的方法不能多个cell同时展开,只能一个一个的展开. 我就尝试用用数组记录展开的标记的方法,功能实现了, 直接上代码: // // Vie ...
- python基础一 day16 内置函数
# 数据类型 :int bool ...# 数据结构 : dict list tuple set str reversed()l = [1,2,3,4,5]l.reverse()print(l)l = ...
- Java AES加密算法工具类
AESCodec.java package util; import java.security.Key; import javax.crypto.Cipher; import javax.crypt ...
- Bootstrap 历练实例 - 折叠(Collapse)插件事件
事件 下表列出了折叠(Collapse)插件中要用到的事件.这些事件可在函数中当钩子使用. 事件 描述 实例 show.bs.collapse 在调用 show 方法后触发该事件. $('#ident ...
- 1207: [HNOI2004]打鼹鼠
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4154 Solved: 1997[Submit][Status][Discuss] Descript ...
- 记python版本管理--pyenv
随记: 众所周知,python2.x版本与3.x版本有比较大的区别,如果你是2.x版本的使用者,突然接了3.x版本的项目,或者反过来,遇到这种情况该怎么办呢?重新安装自己电脑上的python,来匹配对 ...
- rpc - 接口返回数据结构的设计
方案一: 系统级状态 .业务级别的状态同用 code要特殊声明保留状态,如若不声明保留状态,一旦业务开发人员用到了系统级的状态,就有必要侵入的改动业务返回的code(新code与业务欲返回的code ...