-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditDistance.java
More file actions
24 lines (18 loc) · 816 Bytes
/
EditDistance.java
File metadata and controls
24 lines (18 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package dp.commonsubsequence;
import java.util.Arrays;
public class EditDistance {
public static int editDistance(String s, String t) {
int[][] dp = new int[s.length() + 1][t.length() + 1];
for (int[] temp : dp) Arrays.fill(temp, Integer.MAX_VALUE);
return solve(0, 0, s, t, dp);
}
public static int solve(int i, int j, String s, String t, int[][] dp) {
if (i == s.length()) return t.length() - j;
if (j == t.length()) return s.length() - i;
if (dp[i][j] != Integer.MAX_VALUE) return dp[i][j];
if (s.charAt(i) == t.charAt(j)) {
return dp[i][j] = solve(i + 1, j + 1, s, t, dp);
}
return dp[i][j] = 1 + Math.min(solve(i + 1, j, s, t, dp), Math.min(solve(i + 1, j + 1, s, t, dp), solve(i, j + 1, s, t, dp)));
}
}