forked from Yfaye/AlgorithmPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditDistance.java
More file actions
62 lines (52 loc) · 1.87 KB
/
EditDistance.java
File metadata and controls
62 lines (52 loc) · 1.87 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
/*EditDistance.java
Edit Distance
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.)
You have the following 3 operations permitted on a word:
•Insert a character
•Delete a character
•Replace a character
Example
Given word1 = "mart" and word2 = "karma", return 3.
Tags String Dynamic Programming
*/
public class EditDistance {
/**
* @param word1 & word2: Two string.
* @return: The minimum number of steps.
*/
public int minDistance(String word1, String word2) {
// write your code here
if ((word1 == null || word1.length() == 0) && word2 != null) {
return word2.length();
}
if ((word2 == null || word2.length() == 0) && word1 != null) {
return word1.length();
}
if ((word1 == null || word1.length() == 0) && (word2 == null || word2.length() == 0)) {
return 0;
}
int[][] minEdit = new int[word1.length() + 1][word2.length() + 1];
for (int i = 0; i <= word1.length(); i++) {
minEdit[i][0] = i;
}
for (int j = 0; j <= word2.length(); j++) {
minEdit[0][j] = j;
}
for (int i = 1; i <= word1.length(); i++) {
for (int j = 1; j <= word2.length(); j++) {
if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
minEdit[i][j] = minEdit[i - 1][j - 1];
} else {
minEdit[i][j] = 1 + Math.min(minEdit[i - 1][j - 1], Math.min(minEdit[i - 1][j],minEdit[i][j - 1]));
}
}
}
return minEdit[word1.length()][word2.length()];
}
public static void main(String[] args) {
EditDistance test = new EditDistance();
int result = test.minDistance("abba","abbalala");
System.out.println("Expected: 4");
System.out.println(result);
}
}