【Leetcode】583. Delete Operation for Two Strings
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.
题意
给两个单词,每次只能增删一个字符,从一个单词变到另一个需要多少步。
思路
实际上就是求两个字符串的相同部分,再用两个字符串的长度减去公共部分的长度,加和即为需要改变的次数。
本来我给弄成了最长公共子串,结果提交时发现WA了,看了测试用例才发现:
word1: park
word2: spake
结果应该为3,但是用最长公共子串的方式算出来为5,这里是因为应该使用最长公共子序列。
参考:最长公共子序列
代码
public class Solution {
//最长公共子序列长度
public int lcs(String s, String t) {
if (s == null || s.length() == 0 || t == null || t.length() == 0) return 0;
int len1 = s.length(), len2 = t.length();
int[][] dp = new int[len1+1][len2+1];
int max = 0;
for (int i = 0; i <= len1; i++) {
for (int j = 0; j <= len2; j++) {
if (i==0||j==0) dp[i][j] = 0;
else if (s.charAt(i-1) == t.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]);
}
if (max < dp[i][j])
max = dp[i][j];
}
}
return max;
}
public int minDistance(String word1, String word2) {
int common = lcs(word1, word2);
return word2.length() + word1.length() - 2 * common;
}
}

【Leetcode】583. Delete Operation for Two Strings的更多相关文章
- 【LeetCode】583. Delete Operation for Two Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】955. Delete Columns to Make Sorted II
题目如下: We are given an array A of N lowercase letter strings, all of the same length. Now, we may cho ...
- 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】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...
- 【leetcode】740. Delete and Earn
题目如下: Given an array nums of integers, you can perform operations on the array. In each operation, y ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- 【LeetCode】944. Delete Columns to Make Sorted 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...
随机推荐
- C11线程管理:异步操作
1.异步操作 C++11提供了异步操作相关的类,std::future.std::promise和std::package_task.std::future作为异步结果的传输通道,方便的获取线程函数的 ...
- 郑轻校赛 2127 tmk射气球 (数学)
Description 有一天TMK在做一个飞艇环游世界,突然他发现有一个气球匀速沿直线飘过,tmk想起了他飞艇上有一把弓,他打算拿弓去射气球,为了提高射击的准确性,他首先在飞艇上找到一个离气球最近的 ...
- HDU 1214 圆桌会议 (找规律)
题目链接 Problem Description HDU ACM集训队的队员在暑假集训时经常要讨论自己在做题中遇到的问题.每当面临自己解决不了的问题时,他们就会围坐在一张圆形的桌子旁进行交流,经过大家 ...
- Vue修改mint-ui默认样式(默认风格)
加入my-mint.css 我这里用的postcss的变量定义,如果亲们用的是其他预处理器,要改成其他处理器的定义方式 覆盖mint-ui的primary颜色,改为自己UI的主题色 --main-co ...
- three.js_sence(场景)
1,THREE.Scene 的作用 (1)THREE.Scene 对象是所有不同对象的容器,也就是说该对象保存所有物体.光源.摄像机以及渲染所需的其他对象. (2)THREE.Scene 对象又是被称 ...
- 64_q2
qt3-3.3.8b-69.fc26.x86_64.rpm 13-Feb-2017 01:37 3591906 qt3-MySQL-3.3.8b-69.fc26.i686.rpm 13-Feb-201 ...
- MGR Switch Muti-Primary to single_primary
MGR Muti-Primary 切换 single_primary 模式 原因:因为希望做ProxySQL+MGR之间Proxy层的消耗测试,需要把原有的MGR多主改为单主模式. 修改MGRgrou ...
- curl: (6) Couldn’t resolve host ‘www.ttlsa.com’【转】
上周, 部分站点出现Couldn't resolve host.....问题, 导致公司所有走api的程序都无法正常使用(系统redhat 6.3的都出现问题, redhat 5一切OK). 最后解 ...
- Python实现好友全头像的拼接
微信好友全头像 话不多说,直接上代码 import itchat import math import PIL.Image as Image import os itchat.auto_login() ...
- C/C++——[03] 注释
C/C++源程序中被注释的内容不能被编译,被认为是不属于程序的一部分. C/C++的注释有两种写法: 多行注释:以 “ /*”开头,以“ */”结尾: #include <stdio.h> ...