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. Delphi 类的特性

  2. (3)你的第一个python程序

    Life is short,you need python!人生苦短,你需要python!好吧,干了这碗鸡汤................. hello world 没错,几乎是所有程序猿的第一个程 ...

  3. jquery属性方法hasClass判断是否存在某个class

    判断匹配集合中是否存在至少一个元素使用样式'selected',存在则返回'true',不存在为'flase'. <html> <head> <script src=&q ...

  4. jenkins 配置主从机制(master-slaver)

    1. 中文:系统管理——节点管理——新建节点(左上侧) 英文:Manage Jenkins——Manage Node——新建节点(左上侧) 2. 中文配图 英文配图: 3. 远程工作目录 以mac为例 ...

  5. Android Vitals各性能指标介绍

    Android vitals 简介 谷歌推荐使用Android vitals来帮助开发者提高App的稳定性和性能表现. 作为这个方案的一部分, Play Console提供了Android Vital ...

  6. vue多层次组件监听动作和属性

    v-bind="$attrs" v-on="$listeners" Vue 2.4 版本提供了这种方法,将父组件中不被认为 props特性绑定的属性传入子组件中 ...

  7. formData+ajax文件上传

    html代码: <form class="form-horizontal" enctype="multipart/form-data" method=&q ...

  8. python大数据初探--pandas,numpy代码示例

    import pandas as pd import numpy as np dates = pd.date_range(',periods=6) dates import pandas as pd ...

  9. struts2提交表单注意事项 (3)

    供应商模块 需求:实现供应商的列表.添加.修改.删除三个功能 注意:修改时,不允许修改供应商编号 添加时,不允许显现的添加供应商编号 删除时,进行逻辑删除(根据主键将该条数据不再显示在列表) 物理删除 ...

  10. KindEditor完全复制word内容

    我司需要做一个需求,就是使用富文本编辑器时,不要以上传附件的形式上传图片,而是以复制粘贴的形式上传图片. 在网上找了一下,有一个插件支持这个功能. WordPaster 安装方式如下: 直接使用Wor ...