LeetCode Delete Operation for Two Strings
原题链接在这里:https://leetcode.com/problems/delete-operation-for-two-strings/description/
题目:
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.
题解:
找LCS的长度n. word1.length()+word2.length()-2*n. 就是答案.
用DP找LCS的长度. 需要储存的历史信息是到当前点的LCS长度. 用dp[i][j]储存, 表示word1到i和word2到j的LCS长度.
递推时, 若是当前字符match, 在dp[i-1][j-1]的基础上加1即可.
若不match, 取dp[i][j-1] 和 dp[i-1][j]中较大值即可.
初始化都是0.
Time Complexity: O(m*n). m = word1.length(), n = word2.length().
Space: O(m*n).
AC 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){
continue;
}else if(word1.charAt(i-1) == word2.charAt(j-1)){
dp[i][j] = 1 + dp[i-1][j-1];
}else{
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
}
}
}
return len1+len2-2*dp[len1][len2];
}
}
或者像Edit Distance直接计算需要最小的operations数目.
DP问题. 需要储存的历史信息是各自到当前的位置变成相同string需要的最小operation数目. 用二维数组来出巡.
递推时, 若是当前字符match, 不需要额外操作. dp[i][j] = dp[i-1][j-1].
若不match, 需要在dp[i-1][j], dp[i][j-1]中取较小值加1.
初始化或一边在初始位置没动, 最小operation数目就是另一边的位置全部减掉.
答案dp[m][n]. m = word1.length(). n = word2.length().
Time Complexity: O(m*n).
Space: O(m*n).
AC Java:
class Solution {
public int minDistance(String word1, String word2) {
int m = word1.length();
int n = word2.length();
int [][] dp = new int[m+1][n+1];
for(int i = 0; i<=m; i++){
for(int j = 0; j<=n; j++){
if(i == 0 || j == 0){
dp[i][j] = i+j;
}else 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], dp[i][j-1]) + 1;
}
}
}
return dp[m][n];
}
}
也可以降维节省空间.
Time Complexity: O(m*n).
Space: O(n).
AC Java:
class Solution {
public int minDistance(String word1, String word2) {
int m = word1.length();
int n = word2.length();
int [] dp = new int[n+1];
for(int i = 0; i<=m; i++){
int [] temp = new int[n+1];
for(int j = 0; j<=n; j++){
if(i == 0 || j == 0){
temp[j] = i+j;
}else if(word1.charAt(i-1) == word2.charAt(j-1)){
temp[j] = dp[j-1];
}else{
temp[j] = Math.min(dp[j], temp[j-1]) + 1;
}
}
dp = temp;
}
return dp[n];
}
}
类似Longest Common Subsequence, Minimum ASCII Delete Sum for Two Strings, Edit Distance.
LeetCode Delete Operation for Two Strings的更多相关文章
- [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 ...
- 【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 ...
- 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 ...
- [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] 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. ...
随机推荐
- CentOS6升级Apache-httpd2.4.29
本文档解决AppacheHttp由版本2.2.x升级到版本2.4.29的问题,安装需要先进行依赖软件包的安装,请检查相应依赖软件包安装情况,如系统已经正确安装相应依赖软件包,可略过,本文所述版本升级不 ...
- 《Maven实战》第5章 坐标和依赖
5.1 Maven坐标——项目唯一标识 groupId(必须定义):定义Mavan项目隶属的实际项目,如SpringFramework,一个实际项目可包含多个Maven项目 artifactId(必须 ...
- sql行列互换
出现这个结果: sql如下: end) as erjidu from a GROUP BY y;
- Find Min In Rotated Sorted Array2,包含重复数字的反转序列找最小值。
public int findMin(int[] nums) { return findMin(nums, 0, nums.length - 1); } public int findMin(int[ ...
- FTP的安装配置使用
///////////////////////////////FTP///////////////////////////////////////////////////写在前面:在linux 环境下 ...
- java-二维数组——with 刘童格
#include<iostream> #include<string> using namespace std; void MaxIntArray(int a[],int &a ...
- D3.js学习笔记(四)—— 使用SVG坐标空间
目标 在这一章,你将要使用D3.js基于一些数据把SVG元素添加到你想要的坐标位置上. 我们的目标就是使用下面的数据集: var spaceCircles = [30,70,110]; 并使用D3.j ...
- Android TextView 设置滚动条(纯xml)
<ScrollView android:id="@+is/scrollView_id" android:layout_width="fill_parent" ...
- Referenced file contains errors (http://www.springframework.org/schema/aop/spring-aop-3.0.xsd). For more information, right click on the message in th
XML code<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC &q ...
- 随机森林和adaboost算法(待更新)
Adaboost是一种迭代算法,其核心思想是针对同一个训练集训练不同的分类器(弱分类器),然后把这些弱分类器集合起来,构成一个更强的最终分类器(强分类器).