Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3213784
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:59:04+00:00 2026-05-17T14:59:04+00:00

I have the following implementation, but I want to add a threshold, so if

  • 0

I have the following implementation, but I want to add a threshold, so if the result is going to be greater than it, just stop calculating and return.

How would I go about that?

EDIT: Here is my current code, threshold is not yet used…the goal is that it is used

    public static int DamerauLevenshteinDistance(string string1, string string2, int threshold)
    {
        // Return trivial case - where they are equal
        if (string1.Equals(string2))
            return 0;

        // Return trivial case - where one is empty
        if (String.IsNullOrEmpty(string1) || String.IsNullOrEmpty(string2))
            return (string1 ?? "").Length + (string2 ?? "").Length;


        // Ensure string2 (inner cycle) is longer
        if (string1.Length > string2.Length)
        {
            var tmp = string1;
            string1 = string2;
            string2 = tmp;
        }

        // Return trivial case - where string1 is contained within string2
        if (string2.Contains(string1))
            return string2.Length - string1.Length;

        var length1 = string1.Length;
        var length2 = string2.Length;

        var d = new int[length1 + 1, length2 + 1];

        for (var i = 0; i <= d.GetUpperBound(0); i++)
            d[i, 0] = i;

        for (var i = 0; i <= d.GetUpperBound(1); i++)
            d[0, i] = i;

        for (var i = 1; i <= d.GetUpperBound(0); i++)
        {
            for (var j = 1; j <= d.GetUpperBound(1); j++)
            {
                var cost = string1[i - 1] == string2[j - 1] ? 0 : 1;

                var del = d[i - 1, j] + 1;
                var ins = d[i, j - 1] + 1;
                var sub = d[i - 1, j - 1] + cost;

                d[i, j] = Math.Min(del, Math.Min(ins, sub));

                if (i > 1 && j > 1 && string1[i - 1] == string2[j - 2] && string1[i - 2] == string2[j - 1])
                    d[i, j] = Math.Min(d[i, j], d[i - 2, j - 2] + cost);
            }
        }

        return d[d.GetUpperBound(0), d.GetUpperBound(1)];
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T14:59:04+00:00Added an answer on May 17, 2026 at 2:59 pm

    Finally got it…though it’s not as beneficial as I had hoped

        public static int DamerauLevenshteinDistance(string string1, string string2, int threshold)
        {
            // Return trivial case - where they are equal
            if (string1.Equals(string2))
                return 0;
    
            // Return trivial case - where one is empty
            if (String.IsNullOrEmpty(string1) || String.IsNullOrEmpty(string2))
                return (string1 ?? "").Length + (string2 ?? "").Length;
    
    
            // Ensure string2 (inner cycle) is longer
            if (string1.Length > string2.Length)
            {
                var tmp = string1;
                string1 = string2;
                string2 = tmp;
            }
    
            // Return trivial case - where string1 is contained within string2
            if (string2.Contains(string1))
                return string2.Length - string1.Length;
    
            var length1 = string1.Length;
            var length2 = string2.Length;
    
            var d = new int[length1 + 1, length2 + 1];
    
            for (var i = 0; i <= d.GetUpperBound(0); i++)
                d[i, 0] = i;
    
            for (var i = 0; i <= d.GetUpperBound(1); i++)
                d[0, i] = i;
    
            for (var i = 1; i <= d.GetUpperBound(0); i++)
            {
                var im1 = i - 1;
                var im2 = i - 2;
                var minDistance = threshold;
    
                for (var j = 1; j <= d.GetUpperBound(1); j++)
                {
                    var jm1 = j - 1;
                    var jm2 = j - 2;
                    var cost = string1[im1] == string2[jm1] ? 0 : 1;
    
                    var del = d[im1, j] + 1;
                    var ins = d[i, jm1] + 1;
                    var sub = d[im1, jm1] + cost;
    
                    //Math.Min is slower than native code
                    //d[i, j] = Math.Min(del, Math.Min(ins, sub));
                    d[i, j] = del <= ins && del <= sub ? del : ins <= sub ? ins : sub;
    
                    if (i > 1 && j > 1 && string1[im1] == string2[jm2] && string1[im2] == string2[jm1])
                        d[i, j] = Math.Min(d[i, j], d[im2, jm2] + cost);
    
                    if (d[i, j] < minDistance)
                        minDistance = d[i, j];
                }
    
                if (minDistance > threshold)
                    return int.MaxValue;
            }
    
            return d[d.GetUpperBound(0), d.GetUpperBound(1)] > threshold 
                ? int.MaxValue 
                : d[d.GetUpperBound(0), d.GetUpperBound(1)];
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this following codes: @implementation MyImageView @synthesize image; //image is a UIImage -
I have the following code implementation of my generic singleton provider: public sealed class
I have the following own Interface Implementation in my Fragment: @Override public void onReportChanged(Fragment
I have the following scenario.The implementation is required for a real time application. 1)I
I have a problem with the following implementation of hook_cron in Drupal 6.1.3. The
Given I have two File objects I can think of the following implementation: public
I have the following classes/protocols: @interface Super @end @implementation Super @end @interface Sub1 @end
i have the following code, taken partly from a Red Black Tree Java implementation.
I seem to have a error in the following A* pathfinding implementation, which I
I have an implementation of lazyDataModel following this tutorial http://uaihebert.com/?p=1120 My code its a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.