-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path0119. Edit Distance.java
More file actions
32 lines (31 loc) · 1.18 KB
/
0119. Edit Distance.java
File metadata and controls
32 lines (31 loc) · 1.18 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
public class Solution {
/**
* @param word1: A string
* @param word2: A string
* @return: The minimum number of steps.
*/
public int minDistance(String A, String B) {
int blen = B.length();
char[] str = A.toCharArray();
char[] ttr = B.toCharArray();
int[] curr = new int[blen + 1];
for (int b = 1; b <= blen; b++) {
curr[b] = b; //edit distance from "" to that portion of B is b
}
for (int a = 0; a < A.length(); a++) {
// System.out.println(Arrays.toString(curr));
int[] next = new int[blen + 1];
next[0] = a + 1; //distance from "" to that part of A
for (int b = 0; b < blen; b++) {
if (str[a] == ttr[b]) {
next[b + 1] = curr[b];
} else {
next[b + 1] = Math.min(curr[b + 1], curr[b]); //curr[b] represents a replacement
next[b + 1] = Math.min(next[b + 1], next[b]) + 1; //curr[b+1] represnts deletion(last char in A) and next[b] represents insertion(next char in B)
}
}
curr = next;
}
return curr[blen];
}
}