Advertisement
Guest User

muietiganu

a guest
May 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. public class Main {
  2.  
  3. public static void main(String args[]) {
  4.  
  5. char[] a = {'k','i','t','t','e','n'};
  6. char[] b = {'s','i','t','t','i','n','g'};
  7.  
  8. int len_a = a.length;
  9. int len_b = b.length;
  10.  
  11. int result =LevenshteinDistance(a,len_a,b,len_b);
  12.  
  13. System.out.println("Levenshtein Distance:"+result);
  14. }
  15.  
  16. public static int LevenshteinDistance(char a[],int len_a,char b[],int len_b) {
  17.  
  18. int cost;
  19.  
  20. if(len_a == 0)
  21. return len_b;
  22. if(len_b == 0)
  23. return len_a;
  24. if (a[len_a-1] == b[len_b-1])
  25. cost = 0;
  26. else
  27. cost = 1;
  28.  
  29. int substitution = LevenshteinDistance(a,len_a-1,b, len_b) + 1;
  30. int insertion = LevenshteinDistance(a, len_a, b, len_b-1) + 1;
  31. int deletion = LevenshteinDistance(a,len_a-1,b,len_b-1) + cost;
  32.  
  33. return minimum(
  34. substitution,
  35. insertion,
  36. deletion
  37. );
  38. }
  39.  
  40. public static int minimum(int s,int i, int d) {
  41. return Math.min(Math.min(s, i), d);
  42.  
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement