583. Delete Operation for Two Strings
来源:互联网 发布:搜狗关键词优化排名 编辑:程序博客网 时间:2024/06/05 13:29
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: 2Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
Note:
- The length of given words won't exceed 500.
- Characters in given words can only be lower-case letters.
Subscribe to see which companies asked this question.
public class Solution { public int minDistance(String word1, String word2) { int m = word1.length();int n = word2.length();int[][] dp = new int[m + 1][n + 1];for (int i = 0; i <= m; ++i)dp[i][0] = 0;for (int i = 0; i <= n; ++i)dp[0][i] = 0;for (int i = 1; i <= m; ++i) {for (int j = 1; j <= n; ++j) {if (word1.charAt(i - 1) == word2.charAt(j - 1))dp[i][j] = dp[i - 1][j - 1] + 1;elsedp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);}}return m + n - dp[m][n] * 2; }}
阅读全文
0 0
- 583. Delete Operation for Two Strings Medium
- [LeetCode]583. Delete Operation for Two Strings
- 583. Delete Operation for Two Strings
- [leetcode] 583. Delete Operation for Two Strings
- 583. Delete Operation for Two Strings
- 583. Delete Operation for Two Strings
- [leetcode]583. Delete Operation for Two Strings
- leetcode 583. Delete Operation for Two Strings
- [LeetCode] 583. Delete Operation for Two Strings
- 583.Delete Operation for Two Strings
- Delete Operation for Two Strings
- LeetCode:M-583. Delete Operation for Two Strings
- #leetcode编程日记#583. Delete Operation for Two Strings
- [leetcode 583] Delete Operation for Two Strings
- leetcode583. Delete Operation for Two Strings
- 第十三周:( LeetCode583) Delete Operation for Two Strings(c++)
- Delete Operation for Two Strings问题及解法
- Leetcode之Delete Operation for Two Strings 问题
- 利用Java反射机制拼接xml
- Git|从隐藏项中获取指定文件
- IT基础20
- 反射中Class.forName()和ClassLoader.loadClass()的区别
- 套接字(全双工)实现进程间通信
- 583. Delete Operation for Two Strings
- 最长递增子序列
- Spring Boot学习(四)
- springMVC系列(三)——异常统一处理
- ubuntu 安装 repo 记录
- redis操作
- 【转】整理:深度学习 vs 机器学习 vs 模式识别
- Spring详解
- 一步一步学MySQL---18 MySQL常用函数(1)