-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathq072_EditDistance.java
More file actions
79 lines (61 loc) · 2.54 KB
/
q072_EditDistance.java
File metadata and controls
79 lines (61 loc) · 2.54 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package leetcode_algorithm;
/**
* Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
* <p>
* You have the following 3 operations permitted on a word:
* <p>
* a) Insert a character
* b) Delete a character
* c) Replace a character
*/
public class q072_EditDistance {
public static void main(String[] args) {
System.out.println(new q072_EditDistance().minDistance("abc", "edf"));
}
/*
Let following be the function definition :-
f(i, j) := minimum cost (or steps) required to convert first i characters of word1 to first j characters of word2
Case 1: word1[i] == word2[j], i.e. the ith the jth character matches.
f(i, j) = f(i - 1, j - 1)
Case 2: word1[i] != word2[j], then we must either insert, delete or replace, whichever is cheaper
f(i, j) = 1 + min { f(i, j - 1), f(i - 1, j), f(i - 1, j - 1) }
f(i, j - 1) represents insert operation
f(i - 1, j) represents delete operation
f(i - 1, j - 1) represents replace operation
Here, we consider any operation from word1 to word2. It means, when we say insert operation, we insert a new character after word1 that matches the jth character of word2. So, now have to match i characters of word1 to j - 1 characters of word2. Same goes for other 2 operations as well.
Note that the problem is symmetric. The insert operation in one direction (i.e. from word1 to word2) is same as delete operation in other. So, we could choose any direction.
Above equations become the recursive definitions for DP.
Base Case:
f(0, k) = f(k, 0) = k
*/
/**
* ½â·¨1(ÍÆ¼ö½â·¨)
*
* @param word1
* @param word2
* @return
*/
public int minDistance(String word1, String word2) {
int m = word1.length();
int n = word2.length();
int[][] cost = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++)
cost[i][0] = i;
for (int i = 1; i <= n; i++)
cost[0][i] = i;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (word1.charAt(i) == word2.charAt(j))
cost[i + 1][j + 1] = cost[i][j];
else {
int a = cost[i][j];
int b = cost[i][j + 1];
int c = cost[i + 1][j];
cost[i + 1][j + 1] = Math.min(Math.min(a, b), c);
cost[i + 1][j + 1]++;
}
}
}
return cost[m][n];
}
}