1. Job Roadmap
  2. Home
  3. Problem Set
  4. codenotelist
  5. Forum
  6. course
  7. Shore Share Sessions
  8. Record
  1. Login
  2. Sign Up
  3. Language
    1. English
    2. 한국어
    3. 简体中文
    4. 正體中文
    ZhContent TextSol AI分析

解题思路

本题可以转化为求两个字符串的最长公共子序列,即 LCSLCSLCS。

如果 word1word1word1 和 word2word2word2 的最长公共子序列长度为 lcslcslcs,那么:

  • word1word1word1 需要删除 word1.length−lcsword1.length - lcsword1.length−lcs 个字符
  • word2word2word2 需要删除 word2.length−lcsword2.length - lcsword2.length−lcs 个字符

P4903.两个字符串的删除操作(非hot100)

    1000ms Tried: 44 Accepted: 26 Difficulty: 5 所属公司 : Hot100
    算法与标签>动态规划

Leetcode 583.两个字符串的删除操作

题目描述

给定两个单词 word1word1word1 和 word2word2word2。

每步可以删除任意一个字符串中的一个字符。

请返回使得 word1word1word1 和 word2word2word2 相同所需的最少步数。

输入描述

第一行输入一个字符串 word1word1word1。

第二行输入一个字符串 word2word2word2。

输出描述

输出一个整数,表示使 word1word1word1 和 word2word2word2 相同所需的最少删除步数。

样例 111

输入

sea
eat

输出

2

样例解释

第一步将 word1word1word1 中的 seaseasea 删除字符 sss,变为 eaeaea。

第二步将 word2word2word2 中的 eateateat 删除字符 ttt,变为 eaeaea。

因此最少需要 222 步。

样例 222

输入

leetcode
etco

输出

4

数据范围

1<=word1.length,word2.length<=5001 <= word1.length, word2.length <= 5001<=word1.length,word2.length<=500

word1word1word1 和 word2word2word2 只包含小写英文字母。

登录后即可使用 AI 分析。

模式
倒计时时长
:

最长 10 小时 59 分;应用后按此时长重新开始。

提示:点击提交记录在左侧题面区域查看详情
题库
AI分析设置
留空使用官方API Key,每天有次数限制(自定义API Key仅限会员和管理员使用,不限次数)
会员和管理员可切换模型;切到 Kimi/智谱/通义/豆包时需填写对应供应商 API Key
升级会员,可将运行与提交冷却时间缩短至 1 秒起

Status

  • Judging Queue
  • Service Status

Development

  • Open Source

Support

  • Help
  • Contact Us

About

  • About
  • Privacy
  • Terms of Service
  • Copyright Complaint
  1. Language
    1. English
    2. 한국어
    3. 简体中文
    4. 正體中文
  2. Legacy mode
  3. Theme
    1. Light
    2. Dark
  1. 京ICP备2025123107号-1
  2. Worker 0, 48ms
  3. Powered by Hydro v5.0.0-beta.18 Community
CLOSE


ScanQRCodePrompt

请使用微信扫描下方二维码完成注册

Forgot password or username?