/* Labb 2 i DD1352 Algoritmer, datastrukturer och komplexitet    */
/* Se labbanvisning under kurswebben https://www.kth.se/social/course/DD1352 */
/* Ursprunglig författare: Viggo Kann KTH viggo@nada.kth.se      */
import java.util.LinkedList;
import java.util.List;

public class ClosestWords {
  LinkedList<String> closestWords = null;

  int closestDistance = -1;

  int Distance(String w1, String w2, int w1len, int w2len) {
    int i;
    int j;
    int[][] matrix = new int[w1len+1][w2len+1];

    for (i = 0; i <= w1len; i++) {
      matrix[i][0] = i;
    }
    for (j = 0; j <= w2len; j++) {
      matrix[0][j] = j;
    }

    for (i = 1; i <= w1len; i++) {
      for (j = 1; j <= w2len; j++) {
        if (w1.charAt(i-1) == w2.charAt(j-1)) {
          matrix[i][j] = matrix[i-1][j-1];
        } else if (matrix[i-1][j] < matrix[i][j-1]) {
          matrix[i][j] = 1 + matrix[i-1][j];
        } else  {
          matrix[i][j] = 1 + matrix[i][j-1];
        }
      }
    }
    return matrix[w1len][w2len];
  }

  public ClosestWords(String w, List<String> wordList) {
    for (String s : wordList) {
      int dist = Distance(w, s, w.length(), s.length());
      // System.out.println("d(" + w + "," + s + ")=" + dist);
      if (dist < closestDistance || closestDistance == -1) {
        closestDistance = dist;
        closestWords = new LinkedList<String>();
        closestWords.add(s);
      }
      else if (dist == closestDistance)
        closestWords.add(s);
    }
  }

  int getMinDistance() {
    return closestDistance;
  }

  List<String> getClosestWords() {
    return closestWords;
  }
}

@@ -441,24 +441,37 @@
 %0A    int j;%0A
+    int min;%0A
     int%5B%5D%5B%5D 
@@ -506,16 +506,16 @@
 len+1%5D;%0A
-
 %0A    for
@@ -706,24 +706,56 @@
 len; j++) %7B%0A
+        min = matrix%5Bi-1%5D%5Bj-1%5D;%0A
         if (
@@ -769,17 +769,17 @@
 At(i-1) 
-=
+!
 = w2.cha
@@ -805,75 +805,41 @@
    m
-atrix%5Bi%5D%5Bj%5D = matrix%5Bi-1%5D%5Bj-1%5D;%0A        %7D else if (matrix%5Bi-1%5D%5Bj%5D %3C
+in++;%0A        %7D%0A        if (min %3E
  mat
@@ -843,21 +843,21 @@
 matrix%5Bi
+-1
 %5D%5Bj
--1
 %5D) %7B%0A   
@@ -860,35 +860,26 @@
 %0A          m
-atrix%5Bi%5D%5Bj%5D
+in
  = 1 + matri
@@ -902,39 +902,58 @@
    %7D
- else  %7B%0A          matrix%5Bi%5D%5Bj%5D
+%0A        if (min %3E matrix%5Bi%5D%5Bj-1%5D) %7B%0A          min
  = 1
@@ -977,24 +977,52 @@
 ;%0A        %7D%0A
+        matrix%5Bi%5D%5Bj%5D = min;%0A
       %7D%0A    

Fix off-by-one error