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的更多相关文章

  1. 【LeetCode】583. Delete Operation for Two Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【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 ...

  3. 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 ...

  4. [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 ...

  5. 【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...

  6. 【leetcode】740. Delete and Earn

    题目如下: Given an array nums of integers, you can perform operations on the array. In each operation, y ...

  7. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...

  8. 【LeetCode】944. Delete Columns to Make Sorted 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...

随机推荐

  1. HDU6128 二次剩余/二次域求二次剩余解/LL快速乘法取模

    LINK 题意:求满足模p下$\frac{1}{a_i+a_j}\equiv\frac{1}{a_i}+\frac{1}{a_j}$的对数,其中$n,p(1\leq n\leq10^5,2\leq p ...

  2. Python print "hello world" SyntaxError: invalid syntax

    刚安装Python,在IDLE中输入print “Hello World”,谁知却发生错误: >>> print "Hello World"SyntaxError ...

  3. 重构改善既有代码设计--重构手法07:Remove Assignments to Parameters (移除对参数的赋值)

    代码对一个 参数赋值.以一个临时变量取代该参数的位置.     int Discount(int inputVal, int quantity, int yearTodate) { if (input ...

  4. 今日文摘:浅谈 HTML5 的游戏化之路

    如今商业网站中用于广泛的HTML5无限下拉效果已经越来越受到游戏网站的喜爱.各个品牌为了打造专属自己的游戏特色,纷纷推出了模拟HTML5效果的品牌 站,且都起到了相当好的效果.可是从很多方面来说我们对 ...

  5. LintCode 539: Move Zeroes

    终于下决心开始刷题了! 选择LintCode而不是LeetCode主要是因为LintCode操作更顺手,希望能够坚持下去. 还是循序渐进吧,数据结构和算法的东西很久没碰都很生疏了,先找找感觉. 这是一 ...

  6. 【leetcode 简单】第三十一题 买卖股票的最佳时机

    给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 ...

  7. Spring Boot企业级博客系统实战视频教程

    欢迎关注我的微信公众号:"Java面试通关手册" 回复关键字" springboot "免费领取(一个有温度的微信公众号,期待与你共同进步~~~坚持原创,分享美 ...

  8. docker之构建redis-cluster集群

    下载和编译redis安装包 参考:https://www.cnblogs.com/cwp-bg/p/8094914.html # 从官方网站下载安装包,注意,当前在哪个目录下执行命令,下载的包将在哪个 ...

  9. UVA题解一

    UVA 100 题目描述:经典3n+1问题在\(n \leq 10^6\)已经证明是可行的,现在记\(f[n]\)为从\(n\)开始需要多少步才能到\(1\),给出\(L, R\),问\(f[L], ...

  10. 在ubuntu上安装Chrome

    1.下载谷歌浏览器源文件.链接有很多,以下是64位版本的下载地址 https://dl.google.com/linux/direct/google-chrome-stable_current_amd ...