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. Heap(堆)与Stack(栈)的区别详解

    在了解堆与栈之前,我们想来了解下程序的内存分配 一个编译的程序占用的内存分为以下几个部分  :  1.栈区(stack)—   由编译器自动分配释放   ,存放函数的参数值,局部变量的值等.其    ...

  2. Bootstrap treegrid 实现树形表格结构

    前言 :最近的项目中需要实现树形表格功能,由于前端框架用的是bootstrap,但是bootstrapTable没有这个功能所以就找了一个前端的treegrid第三方组件进行了封装.现在把这个封装的组 ...

  3. 【异常】Cannot run program "git" (in directory "/mnt/software/azkaban-3.79.0"): error=2, No such file or directory

    1 安装azkaban异常 cloudera-scm@cdh4 azkaban-3.79.0]$ ./gradlew build -x test Parallel execution with con ...

  4. 共享手机网络给电脑(USB连接)

    华为手机步骤: 设置-->搜索-->hdb-->允许HiSuite通过HDB连接设置 设置-->无线和网络-->移动网络共享-->USB共享网络

  5. intellij idea打包出来的jar包,运行时中文乱码

    比如以下代码: import javax.swing.*; public class addJarPkg { public static void main(String[] args) { JFra ...

  6. P2402 奶牛隐藏 二分+网络流

    floyd搞出两点间最短距离 二分判答案 // luogu-judger-enable-o2 #include<bits/stdc++.h> using namespace std; ty ...

  7. FirstWriting

    在很久很久以前,你拥有我我拥有你 <外面的世界> 在很久很久以前我就有搞一个类似博客的东西的想法,不过一直都没有尝试着搞-- 某天(10号左右吧)刷知乎看到github和hexo可以搭建博 ...

  8. mysql主从同步监控---邮件告警

    #!/bin/bash #check MySQL_Slave Status #crontab time : MYSQLPORT=`netstat -na|grep "|awk -F[:&qu ...

  9. 友善之臂NanoPC T4网络相关设置

    目前(2019年8月)NanoPC T4的桌面系统FriendlyDesktop是基于Ubuntu18.04进行集成的,因此大部分可以参考Ubuntu18.04的配置方法. 1.无线网络配置 可参考官 ...

  10. CentOS开放端口的方法

    Centos升级到7之后,内置的防火墙已经从iptables变成了firewalld.所以,端口的开启还是要从两种情况来说明的,即iptables和firewalld.更多关于CentOs防火墙的最新 ...