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. Spring MVC 拦截器配置 -- 利用session

    spring-servlet.xml <?xml version="1.0" encoding="UTF-8"?><beans xmlns=& ...

  2. 2017 济南精英班 Day1

    不管怎么掰都是n*m-1 #include<cstdio> using namespace std; int main() { freopen("bpmp.in",&q ...

  3. 782B. The Meeting Place Cannot Be Changed 二分 水

    Link 题意:给出$n$个坐标$x_i$,$n$个速度$v_i$问使他们相遇的最短时间是多少. 思路:首先可肯定最终相遇位置必定在区间$[0,max(x_i)]$中,二分最终位置,判断左右部分各自所 ...

  4. spring bean初始化及销毁你必须要掌握的回调方法

    spring bean在初始化和销毁的时候我们可以触发一些自定义的回调操作. 初始化的时候实现的方法 1.通过java提供的@PostConstruct注解: 2.通过实现spring提供的Initi ...

  5. [php]几个常用函数

    count(arr);用于统计数组的元素个数 is_array(arr);判断给定变量是不是数组 var_dump(var||arr);打印数组或变量信息(类型和值): print_r(var||ar ...

  6. phpcms取内容发布管理中的来源

    调取内容发布管理中的来源,如果直接写{$val['copyfrom']}调取出来的内容为   内容|0  ,要先根据“|”进行拆分,然后再写. 示例: <!--新闻开始--> {pc:co ...

  7. 41、用Python实现一个二分查找的函数

    data = [1, 3, 6, 7, 9, 12, 14, 16, 17, 18, 20, 21, 22, 23, 30, 32, 33, 35] def binary_search(dataset ...

  8. Sqlmap注入技巧收集整理

    TIP1 当我们注射的时候,判断注入 http://site/script?id=10http://site/script?id=11-1 # 相当于 id=10http://site/script? ...

  9. 阿里云一键web环境包

    下载地址:https://files.cnblogs.com/files/wordblog/af3a48ef-3a13-479e-85c9-ead61173126c.zip 先把安装包传到服务器上用w ...

  10. 面向过程编程(OPP) 和面向对象编程(OOP)的关系

    面向过程编程(OPP) 和面向对象编程(OOP)的关系 原文链接:http://blog.csdn.net/phphot/article/details/3985480 关于面向过程的编程(OPP)和 ...