# 0583. Delete Operation for Two Strings ###### tags: `Leetcode` `Medium` `Dynamic Programming` `LCS` Link: https://leetcode.com/problems/delete-operation-for-two-strings/ ## 思路 先找到longest common sequence [1143. Longest Common Subsequence](https://hackmd.io/bO7KsaWYS4iKJnjgeCz5gA) 然后用两个字串的长度-2倍lcs的长度 ## Code ```java= class Solution { public int minDistance(String word1, String word2) { int m = word1.length(), n = word2.length(); int[][] dp = new int[m+1][n+1]; dp[0][0] = 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; } else dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]); } } int len = dp[m][n]; return word1.length()+word2.length()-2*len; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up