File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -189,6 +189,31 @@ class Solution {
189
189
}
190
190
}
191
191
```
192
+ ``` java
193
+ // DP - longest common subsequence (用最長公共子序列反推)
194
+ class Solution {
195
+ public int minDistance (String word1 , String word2 ) {
196
+ char [] char1 = word1. toCharArray();
197
+ char [] char2 = word2. toCharArray();
198
+
199
+ int len1 = char1. length;
200
+ int len2 = char2. length;
201
+
202
+ int dp[][] = new int [len1 + 1 ][len2 + 1 ];
203
+
204
+ for (int i = 1 ; i <= len1; i++ ){
205
+ for (int j = 1 ; j <= len2; j++ ){
206
+ if (char1[i - 1 ] == char2[j - 1 ])
207
+ dp[i][j] = dp[i - 1 ][j - 1 ] + 1 ;
208
+ else
209
+ dp[i][j] = Math . max(dp[i - 1 ][j], dp[i][j - 1 ]);
210
+ }
211
+ }
212
+
213
+ return len1 + len2 - (2 * dp[len1][len2]);// 和leetcode 1143只差在這一行。
214
+ }
215
+ }
216
+ ```
192
217
193
218
194
219
Python:
You can’t perform that action at this time.
0 commit comments