Question: How can you compare which strings are closer to one another. Example you have the words hey, zombi , slap which of the following will
How can you compare which strings are closer to one another. Example you have the words "hey", "zombi" , "slap" which of the following will be closer distance to the word zombie.
public class EditDistance {
public static int edit(String s1, String s2) {
int len1 = s1.length();
int len2 = s2.length();
int[][] m = new int[len1 + 1][len2 + 1];
for (int i = 1; i <= len1; i++) {
m[i][0] = i;
}
for (int j = 1; j <= len2; j++) {
m[0][j] = j;
}
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
int cost = s1.charAt(i-1) == s2.charAt(j-1) ? 0 : 1;
m[i][j] = Math.min(Math.min(m[i-1][j] + 1, m[i][j-1] + 1), m[i-1][j-1] + cost);
}
}
return m[len1][len2];
}
public static void main(String[] args) {
String query "Zombie";
String term = {"Hey " ,"Zombi ", " Hola"}
//Use for loop
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
