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. ...
随机推荐
- .net密码找回
using System; using System.Collections.Generic; using System.Text; using System.Net.Mail; using Syst ...
- ASP.NET Dev ASPxGridView控件使用 ASP.NET水晶报表打印
1.ASPxGridView控件使用 2.ASP.NET水晶报表客户端打印 3.javascript打印 4.ASPxGridView根据Textbox查询 5. ASPxGridView 列宽 1. ...
- HDU3308 线段树区间合并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 ,简单的线段树区间合并. 线段树的区间合并:一般是要求求最长连续区间,在PushUp()函数中实 ...
- Trie:字典树
简介 \(Trie\),又称字典树或前缀树,是一种有序树状的数据结构,用于保存关联数组,其中的键值通常是字符串. 作用 把许多字符串做成一个字符串集合,并可以对其进行快速查找(本文以求多少个单词是一个 ...
- 【洛谷2468】[SDOI2010] 粟粟的书架(二合一)
点此看题面 大致题意: 问你选取一个矩形区间内至少几个数,才能使它们的和\(\ge H_i\). 二合一 根据数据范围,比较显然能看出它是一道二合一的题目. 对于第一种情况,\(R,C\le 200\ ...
- Videos
Videos 时间限制: 1 Sec 内存限制: 128 MB提交: 17 解决: 7[提交] [状态] [讨论版] [命题人:admin] 题目描述 C-bacteria takes charg ...
- opencv与灰度图
https://blog.csdn.net/qq_32211827/article/details/56854985 首先,灰度图可以是一个通道存成图片,也可以是3个通道存成图片,3个通道存成图片,其 ...
- 题解 CF656G 【You're a Professional】
又是一道假黑题 它教会我们不要看难度标签 虽然这道题的数据范围很小,用cin能过,但我还是要讲一下快读 快读嘛,顾名思义,就是 快速读入 的意思 有同学就会问了,快速读入的原理是什么? 答:它的原理其 ...
- 如何着手学习一个新的PHP框架
如今的PHP框架层出不穷,名气也各不相同.如何快速掌握一种框架?看看本文吧~ 如今的PHP框架层出不穷,名气也各不相同.我不是这方面的专家,甚至不能熟练地使用其中的一种,所以就不作推荐了.这里我要讨论 ...
- 巧妙使用JQuery Clone 添加多行数据,并更新到数据库
WEB代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="BatchAdd. ...