@@ -51,24 +51,27 @@ public final class OptimalStringAlignment implements StringDistance {
5151 * @return the OSA distance
5252 */
5353 public final double distance (final String s1 , final String s2 ) {
54+ int n = s1 .length (), m = s2 .length ();
55+ if (n == 0 ) return m ;
56+ if (m == 0 ) return n ;
57+
5458
5559 // Create the distance matrix H[0 .. s1.length+1][0 .. s2.length+1]
5660 int [][] d = new int [s1 .length () + 2 ][s2 .length () + 2 ];
5761
58- //@TODO: check special cases
5962 //initialize top row and leftmost column
60- for (int i = 0 ; i <= s1 . length () ; i ++) {
63+ for (int i = 0 ; i <= n ; i ++) {
6164 d [i ][0 ] = i ;
6265 }
63- for (int j = 0 ; j <= s2 . length () ; j ++) {
66+ for (int j = 0 ; j <= m ; j ++) {
6467 d [0 ][j ] = j ;
6568 }
6669
6770 //fill the distance matrix
6871 int cost ;
6972
70- for (int i = 1 ; i <= s1 . length () ; i ++) {
71- for (int j = 1 ; j <= s2 . length () ; j ++) {
73+ for (int i = 1 ; i <= n ; i ++) {
74+ for (int j = 1 ; j <= m ; j ++) {
7275
7376 //if s1[i - 1] = s2[j - 1] then cost = 0, else cost = 1
7477 cost = (s1 .charAt (i - 1 ) == s2 .charAt (j - 1 )) ? 0 : 1 ;
@@ -77,16 +80,19 @@ public final double distance(final String s1, final String s2) {
7780 d [i - 1 ][j - 1 ] + cost , // substitution
7881 d [i ][j - 1 ] + 1 , // insertion
7982 d [i - 1 ][j ] + 1 // deletion
80- );
81-
83+ );
84+
8285 //transposition check
83- if (i > 1 && j > 1 && s1 .charAt (i -1 ) == s2 .charAt (j -2 ) && s1 .charAt (i -2 ) == s2 .charAt (j -1 )){
84- d [i ][j ] = Math .min (d [i ][j ], d [i -2 ][j -2 ] + cost );
86+ if (i > 1 && j > 1
87+ && s1 .charAt (i - 1 ) == s2 .charAt (j - 2 )
88+ && s1 .charAt (i - 2 ) == s2 .charAt (j - 1 )
89+ ){
90+ d [i ][j ] = Math .min (d [i ][j ], d [i - 2 ][j - 2 ] + cost );
8591 }
8692 }
8793 }
8894
89- return d [s1 . length () + 1 ][ s2 . length () + 1 ];
95+ return d [n ][ m ];
9096 }
9197
9298 private static int min (
0 commit comments