// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class WordToWord {
   public static int calc(String w1, String w2) {
      if((w1.length() == 0) && (w2.length() == 0)) {
         // done
         return 0;
      }
      if((w1.length() > 0 )  &&
         (w2.length() > 0) &&
         (w1.charAt(0) == w2.charAt(0))) {
            // match
         return calc(w1.substring(1), w2.substring(1));
      }
      int d,i,u;
      if(w1.length() > 0) {
         // delete 1 char in w1
         d = 1 + calc(w1.substring(1), w2);
      } else {
         d = Integer.MAX_VALUE; 
      }
      if(w2.length() > 0) {
         // insert 1 char in w1
         i = 1 + calc(w1, w2.substring(1));
      } else {
         i = Integer.MAX_VALUE; 
      }
      if((w1.length() > 0) && (w2.length() > 0)) {
         // update 1 char in w1
         u = 1 + calc(w1.substring(1), w2.substring(1));
      } else {
         u = Integer.MAX_VALUE; 
      }
      return Math.min(d, Math.min(i, u));
   }
   public static int orig(String string1, String string2) {
      int m[][] = new int[string1.length() + 1][];
      int i, j;
      for (i = 0; i < string1.length() + 1; i++)
         m[i] = new int[string2.length() + 1];
      m[0][0] = 0;
      for (j = 1; j <= string2.length(); j++)
         m[0][j] = m[0][j - 1] + 1;
      for (i = 1; i <= string1.length(); i++) {
         m[i][0] = m[i - 1][0] + 1;
         for (j = 1; j <= string2.length(); j++) {
            int diag = m[i - 1][j - 1];
            if (string1.charAt(i - 1) != string2.charAt(j - 1))
               diag++;
            m[i][j] = Math.min(diag, Math.min(m[i - 1][j] - 0 + 1, m[i][j - 1] - 0 + 1));
         }
      }
      return m[string1.length()][string2.length()];
   }
   private static void test(String w1, String w2) {
      System.out.println(w1 + "->" + w2 + " = " + calc(w1, w2) + " / " + orig(w1, w2)); 
   }
   public static void main(String[] args) {
      test("hejhej23hej", "hejhejhej");
      test("qwerty", "qwert");
      test("eksperten", "kuglepen");
      test("ekspertensuper", "supereksperten");
   }
}
