forked from line/line-bot-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathWagnerFischer.java
More file actions
45 lines (39 loc) · 1.46 KB
/
WagnerFischer.java
File metadata and controls
45 lines (39 loc) · 1.46 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
package skeleton;
public class WagnerFischer {
private char[] s1;
private char[] s2;
public WagnerFischer(String s1, String s2) {
this.s1 = s1.toLowerCase().toCharArray();
this.s2 = s2.toLowerCase().toCharArray();
}
private int min(int a, int b, int c) {
return Math.min(a, Math.min(b, c));
}
/**
* Using Dynamic Programming, the Wagner-Fischer algorithm is able to
* calculate the edit distance between two strings.
* @return edit distance between s1 and s2
*/
public int getDistance() {
int[][] dp = new int[s1.length + 1][s2.length + 1];
for (int i = 0; i <= s1.length; dp[i][0] = i++);
for (int j = 0; j <= s2.length; dp[0][j] = j++);
for (int i = 1; i <= s1.length; i++) {
for (int j = 1; j <= s2.length; j++) {
if (s1[i - 1] == s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1,
dp[i - 1][j - 1] + 1);
}
}
}
return dp[s1.length][s2.length];
}
public static void main(String[] args) {
WagnerFischer wf = new WagnerFischer("Caffe Mocha", "coffee moka");
System.out.println(wf.getDistance());
wf = new WagnerFischer("Frappuccino", "fappiccino");
System.out.println(wf.getDistance());
}
}