Thought:
http://en.wikipedia.org/wiki/Levenshtein_distance
Code:
public class Solution {
public int minDistance(String word1, String word2) {
int l1 = word1.length(), l2 = word2.length();
int[][] d = new int[l1 + 1][l2 + 1];
for (int i = 0; i < l2 + 1; i++) {
d[0][i] = i;
}
for (int i = 0; i < l1 + 1; i++) {
d[i][0] = i;
}
for (int i = 1; i < l1 + 1; i++) {
for (int j = 1; j < l2 + 1; j++) {
if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
d[i][j] = d[i - 1][j - 1];
}else {
d[i][j] = Math.min(1 + d[i][j - 1], Math.min(1 + d[i - 1][j], 1 + d[i - 1][j - 1]));
}
}
}
return d[l1][l2];
}
}
No comments:
Post a Comment