583. Delete Operation for Two Strings
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.
Example 1:
Input: "sea", "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
Note:
- The length of given words won't exceed 500.
- Characters in given words can only be lower-case letters.
Approach #1: DP. [Java]
class Solution {
public int minDistance(String word1, String word2) {
int len1 = word1.length();
int len2 = word2.length();
int[][] dp = new int[len1+1][len2+1];
for (int i = 0; i <= len1; ++i) {
for (int j = 0; j <= len2; ++j) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
continue;
}
dp[i][j] = word1.charAt(i-1) == word2.charAt(j-1) ? dp[i-1][j-1] + 1 : Math.max(dp[i][j-1], dp[i-1][j]);
}
}
int val = dp[len1][len2];
return len1 - val + len2 - val;
}
}
Analysis:
To make them identical, just find the longest common subsequence. The rest of the characters have to be deleted from the both the strings. which does not belong to longest common subsquence.
Reference:
583. Delete Operation for Two Strings的更多相关文章
- 【Leetcode】583. Delete Operation for Two Strings
583. Delete Operation for Two Strings Given two words word1 and word2, find the minimum number of st ...
- LC 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] 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】583. Delete Operation for Two Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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
原题链接在这里:https://leetcode.com/problems/delete-operation-for-two-strings/description/ 题目: Given two wo ...
- [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 ...
- [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. ...
随机推荐
- 活用RPM获取包的信息
rpm -q 功效大 如果你想要在系统上安装.卸载或是升级软件,需要对系统软件进行查询:或是有如下的场景: 安装了一个软件,需要知道这个软件的版本. 遇到一个文件,不认识它,需要知道它是什么软件,有什 ...
- ffmpeg使用经验
1.工作要使用ffmpeg将视频转换成H264格式,网上查到的很多使用方法都是如下: ffmpeg -i input.mov -c:v libx264 -crf output.mov -i后面表示输入 ...
- playbook
1 --- - hosts: web-server 3 remote_user: root tasks: - name: stop logstash shell: PID=` $PID &&a ...
- ConcurrentModificationException并发修改异常
//创建集合对象 Collection c = new ArrayList(); c.add("hello"); c.add("world"); c.add(& ...
- 坑之mysql 字符串与数字操作
select "123"+1 = 124; select "1a23"+1 = 2; select "aa23"+1 = 1; select ...
- Handler相关
1.延迟方法 Message msg = new Message(); msg.what = 0x111; // netWorkHandler.sendMessage(msg); //延迟方法三 ...
- jquery抓娃娃机代码
<html><head><title>jQuery抓娃娃机游戏代码 - 源码之家</title><meta name="content- ...
- [leetcode]243. Shortest Word Distance最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- 保存一份自己常用的packjson
这里是一份专门针对react的插件配置, 有: es5的转换器,有ie的promise垫片,有蚂蚁金服的anth,还有用于消息通信的pubsub订阅发布系统,虽然现在不用了.... 用于发请求的axi ...
- 如何自行搭建一个威胁感知大脑 SIEM?| 硬创公开课
如何自行搭建一个威胁感知大脑 SIEM?| 硬创公开课 本文作者:谢幺 2017-03-10 10:09 专题:硬创公开课 导语:十年安全产品经验的百度安全专家兜哥,手把手教你用开源项目搭建SIEM安 ...