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:

  1. The length of given words won't exceed 500.
  2. Characters in given words can only be lower-case letters.
 

Runtime: 40 ms, faster than 95.62% of Java online submissions for Delete Operation for Two Strings.

largest subsequence

class Solution {
public int minDistance(String word1, String word2) {
int[][] dp = new int[word1.length()+1][word2.length()+1];
for(int i=1; i<dp.length; i++){
for(int j=1; j<dp[0].length; j++){
if(word1.charAt(i-1) == word2.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]);
}
}
}
return word1.length() + word2.length() - 2 * dp[word1.length()][word2.length()];
}
}

LC 583. Delete Operation for Two Strings的更多相关文章

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

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

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

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

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

  6. LeetCode Delete Operation for Two Strings

    原题链接在这里:https://leetcode.com/problems/delete-operation-for-two-strings/description/ 题目: Given two wo ...

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

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

  9. [LeetCode] 712. 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. ...

随机推荐

  1. 1.JavaWeb 知识点概览

    1.tomcat服务器的安装和配置.http协议 1.1 虚拟目录的 /*映射*/(配置Context元素)(server.xml catalina\localhost\) http://blog.c ...

  2. Delphi 构造和析构

  3. Java错误和异常解析

    Java错误和异常解析 错误和异常 在Java中, 根据错误性质将运行错误分为两类: 错误和异常. 在Java程序的执行过程中, 如果出现了异常事件, 就会生成一个异常对象. 生成的异常对象将传递Ja ...

  4. UTC和GMT时间

    来源:https://www.cnblogs.com/qiuyi21/archive/2008/03/04/1089456.html UTC和GMT时间 每个地区都有自己的本地时间,在网上以及无线电通 ...

  5. ThreadPoolExecutor源码分析一

           在线程池出现之前,每次需要使用线程,都得创建一个线程.但是,在java的运行环境中,创建一个线程是非常耗费资源和时间的.是否可以把线程重复利用,减少线程的创建次数.基于此,java1.5 ...

  6. Eclipse中使用Maven的Jetty插件Debug Web项目

    1.环境配置 JAVA_HOME=D:\Program Files\Java\jdk1.7.0_80 JRE_HOME=%JAVA_HOME%\jre CLASSPATH=.;%JAVA_HOME%/ ...

  7. HDU 6059 - Kanade's trio | 2017 Multi-University Training Contest 3

    思路来自题解(看着题解和标程瞎吉尔比划了半天) /* HDU 6059 - Kanade's trio [ 字典树 ]  |  2017 Multi-University Training Conte ...

  8. 【Android-数据库Sqlite】Sqlite数据库 增、删、改、查

    1.先创建一个Product类 Product.java 变量如下: int id; String code; String name; int qty; 2.创建一个DBHelper类 DBHelp ...

  9. 创建虚拟机(kvm)

    --virsh console virt_name -- virsh nodeinfo                     # 查看kvm节点(服务器)信息 --virsh list [--all ...

  10. 组合模式(Composite)---结构型

    1 基础知识 定义:将对象组合成树形结构以表示“部分-整体”的层次结构.特征:组合模式使得客户端对单个对象和组合对象保持一致的方式处理. 本质:统一叶子对象和组合对象. 目的:让客户端不再区分操作的是 ...