Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created November 6, 2014 07:54
Show Gist options
  • Save kanrourou/f1135f4e8f800deecd07 to your computer and use it in GitHub Desktop.
Save kanrourou/f1135f4e8f800deecd07 to your computer and use it in GitHub Desktop.
public class Solution {
private int min (int a, int b, int c) {
if (a <= b && a <= c)
return a;
else if (b <=a && b <= c)
return b;
else
return c;
}
public int minDistance(String word1, String word2) {
int len1 = word1.length();
int len2 = word2.length();
int[][] arr = new int[len1 + 1][len2 + 1];
arr[0][0] = 0;
for (int i = 1; i <= len1; i++) {
arr[i][0] = i;
}
for (int j = 1; j <= len2; j++) {
arr[0][j] = j;
}
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len2; j++) {
int cost = arr[i][j];
if (word1.charAt(i) != word2.charAt(j)) {
cost++;
}
arr[i + 1][j + 1] = min(arr[i][j + 1] + 1, arr[i + 1][j] + 1, cost);
}
}
return arr[len1][len2];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment