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.

Approach #1: DP. [Java]

class Solution {
public int minDistance(String word1, String word2) {
int len1 = word1.length();
int len2 = word2.length();
int[][] dp = new int[len1+1][len2+1];
for (int i = 0; i <= len1; ++i) {
for (int j = 0; j <= len2; ++j) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
continue;
}
dp[i][j] = word1.charAt(i-1) == word2.charAt(j-1) ? dp[i-1][j-1] + 1 : Math.max(dp[i][j-1], dp[i-1][j]);
}
}
int val = dp[len1][len2];
return len1 - val + len2 - val;
}
}

  

Analysis:

To make them identical, just find the longest common subsequence. The rest of the characters have to be deleted from the both the strings. which does not belong to longest common subsquence.

Reference:

https://leetcode.com/problems/delete-operation-for-two-strings/discuss/103214/Java-DP-Solution-(Longest-Common-Subsequence)

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

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

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

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

  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. CSS vertical-align属性详解

    . 首页 博客园 联系我 前言:关于vertical-align属性. 实践出真知. 垂直居中. 第二种用法. 留言评论 返回顶部 前言:关于vertical-align属性 vertical-ali ...

  2. spark-1

    先测试搭好的spark集群: 本地模式测试: 在spark的目录下: ./bin/run-example SparkPi 10 --master local[2] 验证成功: 集群模式 Spark S ...

  3. Oracle,cast函数

    cast(要转换的值 AS 转换的类型): 问题:'            ' as FSubBillNo 若用此法 oracle 默认字段类型为char型 且字段长度度为输入空格的长度,会导致字符串 ...

  4. linux百万并发之 tcp_mem

    百万并发之 tcp_mem   在服务端,连接达到一定数量,诸如50W时,有些隐藏很深的问题,就不断的抛出来. 通过查看dmesg命令查看,发现大量TCP: too many of orphaned ...

  5. as3.0 当fla里面有TLF文本的时候,加载声音会出现错误

    问题描述 1.现有制作好的mp3加载包,这个包是相对路径 2.如果fla里面没有TLF文本,可以正常运行 解题思路 1.音频的相对路径和加载TLF文本的路径不一样,fla会优先选择TLF文件,这样mp ...

  6. KALI安装与环境配置

    2018-2019 201899224<网络攻防实践>第二周作业 虚拟化网络攻防实验环境包括以下部分: 靶机:包含系统和应用程序安全漏洞,并作为攻击目标的主机.(Windows XP和Li ...

  7. WordPress 自动初始化数据库

    背景 自动化搭建开发环境.测试.部署如通过网页操作(访问 /wp-admin/install.php)相对比较麻烦且在有的场景无法实现. 步骤 修改 wp-config.php 配置 wordpres ...

  8. Linux-目录结构及文件系统

    1.Linux 系统的顶层目录结构 /              根目录 ├── bin     存放用户二进制文件 ├── boot    存放内核引导配置文件 ├── dev     存放设备文件 ...

  9. 通过类名或者jar名查询所在jar包

    一.问题 例如我想查看一下FilterSecurityInterceptor的源码,但是我不知道它在maven依赖中的哪个jar包中 二.解决方案 http://www.findmaven.net/ ...

  10. C++中的仿函数

    仿函数:实质就是重载了小括号(),通过类,定义一个对象,对象可以被实例化,具有内存可以存储数据,把需要比较的数据事先给到类对象的成员,这样在比较两个值的时候,可以只传入需要被比较的值即可.因为比较的值 ...