Does your company or startup need an expert? If so, write to me to talk about possibilities of professional cooperation.
PROGRAMMING on-line course is approaching! Write to me if you are interested in free early access.

MARCIN.com

Marcin Jamro, PhD, DSc

How to calculate Levenshtein distance?

C#  |  .NET  |  Algorithms

The Levenshtein distance is used to measure a difference between two strings, which is interpreted as the number of insertions, deletions and substitutions required to transform the source string into the target one. For example, if you want to change word Marcin into Martine, you need to substitute c with t and add e at the end, so the Levenshtein distance is equal to 2.

The implementation can use an auxiliary two-dimensional array. Its each element (with i and j indices) stores a distance between the i first characters of the input string to the j first characters of the target string. The code is presented below:

int levenshtein(string x, string y)
{
    int[,] t = new int[x.Length + 1, y.Length + 1];

    // Convert i-length input string to empty string, by removing all chars.
    for (int i = 0; i <= x.Length; i++) { t[i, 0] = i; }

    // Convert empty string to j-length target string, by adding j chars.
    for (int j = 1; j <= y.Length; j++) { t[0, j] = j; }
    
    // Calculate distances between i first chars of input string
    // and the j first chars of target string.
    for (int i = 1; i <= x.Length; i++)
    {
        for (int j = 1; j <= y.Length; j++)
        {
            int deletion = t[i - 1, j] + 1;
            int insertion = t[i, j - 1] + 1;
            int replaceCost = x[i - 1] != y[j - 1] ? 1 : 0;
            int replace = t[i - 1, j - 1] + replaceCost;
            t[i, j] = Math.Min(Math.Min(deletion, insertion), replace);
        }
    }

    return t[x.Length, y.Length];
}

You can check this code by the following lines:

int distance = levenshtein("marcin", "martine");
Console.WriteLine($"Distance is equal to {distance}.");

The result is shown as follows:

Distance is equal to 2.

The content, including any part of code, is presented without warranty, either express or implied. The author cannot be held liable for any damages caused or alleged to have been caused directly or indirectly by any content shown on this website.

Hello, I am Marcin

Reliable entrepreneur with 10+ years of companies operation, such as CEO at a few IT companies. I was an author of a few software & hardware products, still open to new ideas & cooperation.

Helpful expert with 10+ years of experience, together with PhD and DSc in Computer Science. I was an author of books and publications, as well as an expert in international projects.

Experienced developer with 10+ years of development and 100+ completed projects. I worked on various complex international projects, e.g. at Microsoft in USA and as CTO at a few companies. I have MCP, MCTS and MCPD certificates.

You can read more about me in my short bio. I am waiting for contact at [email protected], as well as at my Facebook and LinkedIn profiles.

Books

I am an author of a few books and numerous publications, also in high-quality international scientific journals.

If you want to learn data structures and algorithms in the context of C#, let's take a look at my newest book. It is the second edition of C# Data Structures and Algorithms. You can buy it here.

Projects

Do you like traveling? If so, discover an amazing world with local guides, right here and right now at: https://camaica.com

Do you follow your favorite creators? If so, check it out what products they recommend and buy them at: https://refspace.com

Travels

I love travels, learning new cultures and regions, meeting outstanding people, as well as taking pictures of beautiful places. Take a look at my travel diary.