【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 ...
随机推荐
- CF844 C 置换 水
由于每个数字只出现一次,离散化一下,置换求个循环节就好了. /** @Date : 2017-08-25 01:39:39 * @FileName: C.cpp * @Platform: Window ...
- dubbo 响应超时异常: com.alibaba.dubbo.remoting.TimeoutException: Waiting server-side response timeout.
因为dubbo默认的时间是500ms,超过这个时间它会重新请求服务层,最多尝试三次. 如果数据量比较大就不行了显示出来的异常为timeout. 在服务提供端设置timeout=1200000 并且加了 ...
- 拦截asp.net输出流做处理
本文标题是指对已经生成了HTML的页面做一些输出到客户端之前的处理. 方法的原理是:把Response的输出重定向到自定义的容器内,也就是我们的StringBuilder对象里,在HTML所有的向页面 ...
- python笔记之BytesIO
1. 什么是BytesIO BytesIO与StringIO类似,不同的是StringIO只能存放string,BytesIO是用来存放bytes的,它提供了在内存中读写字节的能力. 即在内存中读写字 ...
- FastStoneCapture(FSCapture)录屏、剪辑教程
FastStoneCapture软件编辑视频的使用方法: http://www.tudou.com/programs/view/2eD-s5HP1xw/
- sqlite3在Linux下的安装和使用
自我补充:ubuntu在线安装sqlite3数据库的方法: 系统平台:ubuntu12.04 在ubuntu里面直接使用命令:sudo apt-get install sqlite3 ,出现: ...
- Mutex, semaphore, spinlock的深度解析
Mutex是一把钥匙,一个人拿了就可进入一个房间,出来的时候把钥匙交给队列的第一个.一般的用法是用于串行化对critical section代码的访问,保证这段代码不会被并行的运行. Semaphor ...
- 大型网站的 HTTPS 实践(一)—— HTTPS 协议和原理(转)
原文链接:http://op.baidu.com/2015/04/https-s01a01/ 1 前言 百度已经于近日上线了全站 HTTPS 的安全搜索,默认会将 HTTP 请求跳转成 HTTPS.本 ...
- postman中 form-data、x-www-form-urlencoded、raw、binary的区别 && 下载文件
1.form-data: 就是http请求中的multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开.既可以上传键值对,也可以上传文件.当上传的字段是文件 ...
- python 面试
知识总结 面试(一)