Edit Distance (editing distance). NET implementation

Source: Internet
Author: User
    <summary>///Calculate Text Edit Distance Utility Class///</summary> public static CLA SS Texteditdistanceutility {///<summary>///get edit distance between two string///
        </summary>///<param name= "str1" ></param>///<param name= "str2" ></param>
            <returns></returns> public static int geteditdistance (string str1, String str2) {
            if (str1 = = str2) return 0;
            else if (String.IsNullOrEmpty (str1) && string.isnullorempty (str2)) return 0; else if (String.IsNullOrEmpty (STR1) &&! String.IsNullOrEmpty (STR2)) return str2.
            Length; else if (! String.IsNullOrEmpty (str1) && string.isnullorempty (str2)) return str1.
            Length; int[,] D = new int[str1. Length + 1, str2.
            Length + 1];
            D.initialize ();
            int cost = 0; for (int i = 0; i < D.getLength (0);
            i++) {d[i, 0] = i;
            for (int j = 0; J < d.getlength (1); J + +) {d[0, j] = J; for (int i = 0; i < str1. Length; i++) {for (int j = 0; J < str2. Length;
                    J + +) {if (str1[i] = = Str2[j]) cost = 0;
                    else cost = 1;
                D[i + 1, j + 1] = Math.min (Math.min (D[i, J + 1] + 1, D[i + 1, j] + 1), math.min (D[i + 1, j] + 1, d[i, J] + cost); } return D[STR1. Length, str2.
        Length]; }
    }


Edit distance is to compare the number of basic operations between two strings to become the other operation, add a character, delete a character, modify a character, are counted as an operation

For example ABB and BBB, edit distance is 1,abc into AA, edit distance is 2

The application of this algorithm is more than two times the difference between the results of how much

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.