...
This commit is contained in:
31
compiler/NubLang/Utils.cs
Normal file
31
compiler/NubLang/Utils.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
namespace NubLang;
|
||||
|
||||
public static class Utils
|
||||
{
|
||||
public static int LevenshteinDistance(string a, string b)
|
||||
{
|
||||
if (a == b) return 0;
|
||||
if (a.Length == 0) return b.Length;
|
||||
if (b.Length == 0) return a.Length;
|
||||
|
||||
var costs = new int[b.Length + 1];
|
||||
for (int j = 0; j <= b.Length; j++) costs[j] = j;
|
||||
|
||||
for (int i = 1; i <= a.Length; i++)
|
||||
{
|
||||
costs[0] = i;
|
||||
int prevCost = i - 1;
|
||||
for (int j = 1; j <= b.Length; j++)
|
||||
{
|
||||
int currentCost = costs[j];
|
||||
costs[j] = Math.Min(
|
||||
Math.Min(costs[j - 1] + 1, costs[j] + 1),
|
||||
prevCost + (a[i - 1] == b[j - 1] ? 0 : 1)
|
||||
);
|
||||
prevCost = currentCost;
|
||||
}
|
||||
}
|
||||
|
||||
return costs[b.Length];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user