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. ...
随机推荐
- php获取微信的openid
https://www.cnblogs.com/wxfallstar/p/6826886.html https://www.cnblogs.com/liangxiblog/p/5909432.html
- Linux命令:mapfile
mapfile [-n 计数] [-O 起始序号] [-s 计数] [-t] [-u fd] [-C 回调] [-c 量子] [数组] 两个作用: 一是给index数组读取标准输入来赋值:二是文件描述 ...
- C# 数值的隐式转换
Debug2.Log(5/8.0f, 5.0f/8, 5/8);//output:0.625, 0.625, 0 隐式数值转换表
- 2017-11-04 Sa Oct 消参
2017-11-04 Sa $ P(-3, 0) $ 在圆C $ (x-3)^2 + y^2 = 8^2 $ 内,动圆M与圆相切且过P点,求M点轨迹. 设切点 $ A(a, b) $,圆心 \(M(x ...
- Apache 修改端口号
默认是80端口,可在httpd.conf文件中修改以下2个地方的端口号为预定的即可,例如修改为8080端口 把httpd.conf 中Listen 80 改成Listen 8080 把ServerNa ...
- 搭建EOS环境
[搭建EOS环境] 1.Ubuntu 18.04 下安装eosio wget https://github.com/eosio/eos/releases/download/v1.4.4/eosio_1 ...
- 相机标定——OpenCV-Python Tutorials
原文地址http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_calib3d/py_calibration/py ...
- vue获取后台图片验证码,并点击刷新验证码
<--url为需要访问的接口地址--> <span style="display: inline-block;width: 130px;height: 53px;borde ...
- 43 【redis cluster】
有两篇文章不错,可以看下: 1,初步理解redis cluster:https://blog.csdn.net/dc_726/article/details/48552531 2,仔细理解redis ...
- Android,重新出发!
接触Android差不多一年,工作也有快半年的时间了. 现在回头看看自己写的代码,虽然功能可以实现,但是还是有很多不足的地方. 比如代码结构,一个类中的代码过多,知道这样做不好,但是不知道怎么把这些 ...