forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
34 lines (32 loc) · 1.12 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package g0001_0100.s0072_edit_distance;
// #Medium #Top_100_Liked_Questions #String #Dynamic_Programming
// #Algorithm_II_Day_18_Dynamic_Programming #Dynamic_Programming_I_Day_19
// #Udemy_Dynamic_Programming #Top_Interview_150_Multidimensional_DP #Big_O_Time_O(n^2)_Space_O(n2)
// #2024_11_11_Time_3_ms_(97.19%)_Space_43.2_MB_(98.23%)
@SuppressWarnings("java:S2234")
public class Solution {
public int minDistance(String w1, String w2) {
int n1 = w1.length();
int n2 = w2.length();
if (n2 > n1) {
return minDistance(w2, w1);
}
int[] dp = new int[n2 + 1];
for (int j = 0; j <= n2; j++) {
dp[j] = j;
}
for (int i = 1; i <= n1; i++) {
int pre = dp[0];
dp[0] = i;
for (int j = 1; j <= n2; j++) {
int tmp = dp[j];
dp[j] =
w1.charAt(i - 1) != w2.charAt(j - 1)
? 1 + Math.min(pre, Math.min(dp[j], dp[j - 1]))
: pre;
pre = tmp;
}
}
return dp[n2];
}
}