mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 18:32:56 +08:00
docs(DP): update LevenshteinDistance.java
This commit is contained in:
@ -1,54 +1,53 @@
|
|||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @author Kshitij VERMA (github.com/kv19971)
|
* @author Kshitij VERMA (github.com/kv19971)
|
||||||
* LEVENSHTEIN DISTANCE dyamic programming implementation to show the difference between two strings (https://en.wikipedia.org/wiki/Levenshtein_distance)
|
* LEVENSHTEIN DISTANCE dyamic programming implementation to show the difference between two strings (https://en.wikipedia.org/wiki/Levenshtein_distance)
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class LevenshteinDistance{
|
public class LevenshteinDistance {
|
||||||
private static int minimum(int a, int b, int c){
|
private static int minimum(int a, int b, int c) {
|
||||||
if(a < b && a < c){
|
if (a < b && a < c) {
|
||||||
return a;
|
return a;
|
||||||
}else if(b < a && b < c){
|
} else if (b < a && b < c) {
|
||||||
return b;
|
return b;
|
||||||
}else{
|
} else {
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private static int calculate_distance(String a, String b){
|
|
||||||
|
private static int calculate_distance(String a, String b) {
|
||||||
int len_a = a.length() + 1;
|
int len_a = a.length() + 1;
|
||||||
int len_b = b.length() + 1;
|
int len_b = b.length() + 1;
|
||||||
int [][] distance_mat = new int[len_a][len_b];
|
int[][] distance_mat = new int[len_a][len_b];
|
||||||
for(int i = 0; i < len_a; i++){
|
for (int i = 0; i < len_a; i++) {
|
||||||
distance_mat[i][0] = i;
|
distance_mat[i][0] = i;
|
||||||
}
|
}
|
||||||
for(int j = 0; j < len_b; j++){
|
for (int j = 0; j < len_b; j++) {
|
||||||
distance_mat[0][j] = j;
|
distance_mat[0][j] = j;
|
||||||
}
|
}
|
||||||
for(int i = 0; i < len_a; i++){
|
for (int i = 0; i < len_a; i++) {
|
||||||
for(int j = 0; i < len_b; j++){
|
for (int j = 0; i < len_b; j++) {
|
||||||
int cost;
|
int cost;
|
||||||
if (a.charAt(i) == b.charAt(j)){
|
if (a.charAt(i) == b.charAt(j)) {
|
||||||
cost = 0;
|
cost = 0;
|
||||||
}else{
|
} else {
|
||||||
cost = 1;
|
cost = 1;
|
||||||
}
|
}
|
||||||
distance_mat[i][j] = minimum(distance_mat[i-1][j], distance_mat[i-1][j-1], distance_mat[i][j-1]) + cost;
|
distance_mat[i][j] = minimum(distance_mat[i - 1][j], distance_mat[i - 1][j - 1], distance_mat[i][j - 1]) + cost;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return distance_mat[len_a-1][len_b-1];
|
return distance_mat[len_a - 1][len_b - 1];
|
||||||
|
|
||||||
}
|
}
|
||||||
public static void main(String [] args){
|
|
||||||
|
public static void main(String[] args) {
|
||||||
String a = ""; // enter your string here
|
String a = ""; // enter your string here
|
||||||
String b = ""; // enter your string here
|
String b = ""; // enter your string here
|
||||||
|
|
||||||
System.out.print("Levenshtein distance between "+a + " and "+b+ " is: ");
|
System.out.print("Levenshtein distance between " + a + " and " + b + " is: ");
|
||||||
System.out.println(calculate_distance(a,b));
|
System.out.println(calculate_distance(a, b));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user