-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterative.java
More file actions
37 lines (27 loc) · 791 Bytes
/
Iterative.java
File metadata and controls
37 lines (27 loc) · 791 Bytes
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
public class Iterative extends CharactersDistance {
public Iterative() {
this.implementation = "itérative";
}
public double calculate(String c1, String c2) {
double[][] C = new double[c1.length() + 1][c2.length() + 1];
double d1, d2, d3;
for (int j = 0; j < c2.length() + 1; j++) {
for (int i = 0; i < c1.length() + 1; i++) {
this.counter++;
if (i == 0) {
C[i][j] = j;
}
else if (j == 0) {
C[i][j] = i;
}
else {
d1 = C[i - 1][j] + Operation.ADD.getCost();
d2 = C[i][j - 1] + Operation.DELETE.getCost();
d3 = C[i - 1][j - 1] + (c1.charAt(i - 1) == c2.charAt(j - 1) ? 0 : Operation.TRANSFORM.getCost());
C[i][j] = Math.min(Math.min(d1, d2), d3);
}
}
}
return C[c1.length()][c2.length()];
}
}