Skip to content

Instantly share code, notes, and snippets.

@yasinkuyu
Last active February 18, 2019 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasinkuyu/11350516 to your computer and use it in GitHub Desktop.
Save yasinkuyu/11350516 to your computer and use it in GitHub Desktop.
Super Fast and Accurate string distance algorithm: Sift3 in C#
// @yasinkuyu
// 27/04/2014
public static float Sift3(string s1, string s2, int maxOffset)
{
if (string.IsNullOrEmpty(s1))
return ((string.IsNullOrEmpty(s2)) ? 0 : s2.Length);
if (string.IsNullOrEmpty(s2))
return s1.Length;
int c1 = 0, c2 = 0, lcs = 0;
while ((c1 < s1.Length) && (c2 < s2.Length))
{
if (s1[c1] == s2[c2])
{
lcs++;
}
else
{
for (int i = 1; i < maxOffset; i++)
{
if ((c1 + i < s1.Length) && (s1[c1 + i] == s2[c2]))
{
c1 += i;
break;
}
if ((c2 + i < s2.Length) && (s1[c1] == s2[c2 + i]))
{
c2 += i;
break;
}
}
}
c1++;
c2++;
}
return ((s1.Length + s2.Length) / 2 - lcs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment