SHARE
TWEET

muietiganu

a guest May 21st, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top