Leetcode calculation of string similarity or editing distance editdistance

Source: Internet
Author: User

Problem Description:

/**
 * Given, words word1 and word2, find the minimum number of steps required to
 * Convert Word1 to Word2. (Each operation is counted as 1 step.)
 * * You had the 
 following 3 operations permitted on a word:
 * 
 * a) Insert a character 
 * b) Delete a Chara Cter 
 * c) Replace a character
 */

The similarity of two strings is to calculate the number of steps required to convert a string into another string steps, inserting a character in one step, deleting one character as one step, and substituting one character for one step. So it's also called editing distance.
It is obvious that this is a dynamic programming problem. Declares an array arr[i][j] that represents the distance from the string word1[0...i-1] to word2[0...j-1].
There is a relationship between arr[i+1][j+1] and Arr[i][j], such as the word1 end character is the x,word2 end character is y:
If x=y, then arr[i+1][j+1]=arr[i][j];
If x!=y, then we can remove an element from the word1, arr[i+1][j+1]=arr[i+1][j]+1;
We can also insert an element from Word1, arr[i+1][j+1]=arr[i][j+1]+1;
We can also replace an element from Word1, arr[i+1][j+1]=arr[i][j]+1;
We can use the following figure to explain the above formula:

    A P p l e
  0 1 2 3 4 5
a 1 0 1 2 3 4
P 2 1 0 1 2 3
P 3 2 1 0 1 2

The first thing to initialize the arr array, arr[i][0]=i,arr[0][j]=j, is that the meaning of the initialization is arr[i][0]=i, that is, if the Word2 is empty, then word2 into the app, 0,1,2,3 steps are required, respectively. That is, word2 increases ' a ', increases ' p ', increases ' p '.
ARR[0][J] says that word1 from No to Word2 "Apple" needs 0,1,2,3,4,5 step, that is, increase ' a ', increase ' p ', increase ' p ', add ' l ', add ' e '.

One of Word1 's characters wants to be converted into WORD2, there are 3 ways: Replace, delete, add

Add 1 to each conversion, as this counts as 1 steps. This solves the above three formulas.

The specific code is as follows:

public int Mindistance (string word1, String word2) {if (word1.length () = = 0 | | word2.length () = = 0) r Eturn word1.length () = = 0?
        Word2.length (): Word1.length ();
        int[][] arr = new Int[word1.length () + 1][word2.length () + 1];
        for (int i = 0; I <= word1.length (); i++) {arr[i][0] = i;
        } for (int j = 0, J <= Word2.length (); j + +) {Arr[0][j] = j;
                } for (int i = 0; i < word1.length (), i++) {for (int j = 0; J < Word2.length (); + j) {
                if (Word1.charat (i) = = Word2.charat (j)) {arr[j + 1][i + 1] = Arr[j][i];
                    } else {int replace = arr[i][j]+1;
                    int insert = Arr[i][j + 1]+1;

                    int delete = Arr[i + 1][j]+1; int min = replace > insert?
                    Insert:replace; min = delete > min?
                    Min:delete; Arr[i + 1][j + 1] = min;
    }}} return Arr[word1.length ()][word2.length ()]; }

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.