Advertisement
teleias

LevenshteinDistance

Mar 14th, 2023
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1.     public static int ComputeDistance(string a, string b)
  2.     {
  3.         int length1 = a.Length;
  4.         int length2 = b.Length;
  5.         if (length1 == 0)
  6.             return length2;
  7.         if (length2 == 0)
  8.             return length1;
  9.         int[,] numArray = new int[length1 + 1, length2 + 1];
  10.         for (int index1 = 1; index1 <= length1; ++index1)
  11.         {
  12.             for (int index2 = 1; index2 <= length2; ++index2)
  13.             {
  14.                 int num = (int) b[index2 - 1] == (int) a[index1 - 1] ? 0 : 1;
  15.                 numArray[index1, index2] = Math.Min(Math.Min(numArray[index1 - 1, index2] + 1, numArray[index1, index2 - 1] + 1), numArray[index1 - 1, index2 - 1] + num);
  16.             }
  17.         }
  18.         return numArray[length1, length2];
  19.     }
  20.  
  21.     public static string TryGetClosestString(string input, List<string> options)
  22.     {
  23.         if (options.Count == 0)
  24.             return "";
  25.         int num1 = int.MaxValue;
  26.         string option = options[0];
  27.         for (int index = 0; index < options.Count; ++index)
  28.         {
  29.             int num2 = ComputeDistance(input, options[index]);
  30.             if (num2 < num1)
  31.             {
  32.                 option = options[index];
  33.                 num1 = num2;
  34.             }
  35.         }
  36.         return option;
  37.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement