-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
36 lines (26 loc) · 1 KB
/
Copy pathSolution.java
File metadata and controls
36 lines (26 loc) · 1 KB
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
35
36
class Solution {
public int minDistance(String word1, String word2) {
char[] arr1 = word1.toCharArray();
char[] arr2 = word2.toCharArray();
Integer[][] memo = new Integer[arr1.length + 1][arr2.length + 1];
return findMin(arr1, arr2, arr1.length, arr2.length, memo);
}
private int findMin(char[] arr1, char[] arr2, int i, int j, Integer[][] memo) {
if (memo[i][j] != null) {
return memo[i][j];
}
if (i == 0 || j == 0) {
memo[i][j] = i == 0 ? j : i;
return memo[i][j];
}
if (arr1[i - 1] == arr2[j - 1]) {
memo[i][j] = findMin(arr1, arr2, i - 1, j - 1, memo);
} else {
int insert = findMin(arr1, arr2, i, j - 1, memo);
int delete = findMin(arr1, arr2, i - 1, j, memo);
int replace = findMin(arr1, arr2, i - 1, j - 1, memo);
memo[i][j] = 1 + Math.min(insert, Math.min(delete, replace));
}
return memo[i][j];
}
}