Leetcode Edit Distance

Source: Internet
Author: User

First give the topic:

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 have the following 3 operations permitted on a word:

A) Insert a character
b) Delete a character
c) Replace a character

When encountering this problem, the first thought of the solution is backtracing, that is, the search for the entire solution space of violence, to find the optimal solution. Result timed out

Baidu a bit, know that the problem is the use of dynamic programming solution.

Since it is dynamic programming, the key of solution is the state transfer equation. I had thought about dynamic planning before, but I didn't.

The state transfer equation considered at that time was

Designed according to the length of the word1, that is, dp[i] represents the minimum number of steps required for the WORD1 string substring (0,i) operation to Word2. But this

The problem with the state transition equation is that there is actually only one solution, which is to insert from the empty string until it becomes word2.

However, the correct design method is two-dimensional.

DP[I][J] Represents the minimum number of steps required to convert word1.substring (0,i) to Word2.substring (0,J).

Then we'll have a state transfer dp[i+1][j+1]=min{.

1.dp[i-1][j]+1

2.dp[i][j-1]+1

3.dp[i-1][j-1]+f (I,J)

}

Explain in turn

1. We know that dp[i-1][j] represents the minimum number of steps required to convert word1.substring (0,i-1) to Word2.substring (0,J).

For example, word1= "ABCDE", word2= "XYZHG", i=3,j=4;

The transition from ABC to XYZHG requires at least k steps, so how many steps does it take to convert from ABCD to XYZHG?

Then the ABCD D removed, get ABC, and ABC to XYZHG at least to K-step, so through the deletion operation, can be achieved from ABCD to XYZHG at least k+1 step

2. In the same vein as 1, a minimum of k steps is required to change from ABCD to Xyzh, so just change the ABCD to Xyzh and then insert a G to XYZHG.

3.F (I,J) indicates that if (Word1[i]==word2[j]) return 0; else return 1;

That is, if the current index, two pending characters are equal, then dp[i][j]=dp[i-1][j-1], conversely, a replace operation is required.

After getting this set of state transfer equations, the problem is simple.

The code is given below

public class Solution {/** * @param args */public int mindistance (string word1, String word2) {if (Word1.equals ("") && Amp;word2.equals ("")) return 0; int row=word1.length () +1;  int col=word2.length () +1;  int [][]dp=new Int[row][col];  for (int i=0;i<row;i++)  {  dp[i][0]=i;  }  for (int i=0;i<col;i++)  {  dp[0][i]=i;  }  for (int i=1;i<row;i++) for  (int j=1;j<col;j++)  {  if (Word1.charat (i-1) ==word2.charat (j-1))  Dp[i][j]=dp[i-1][j-1];  else  dp[i][j]=dp[i-1][j-1]+1;  Dp[i][j]=min (dp[i][j],dp[i-1][j]+1,dp[i][j-1]+1);    }  return DP[ROW-1][COL-1];    } public int min (int a1,int a2,int A3) {if (A1<A2) {if (A1<A3) return A1; else return A3;  } else {if (A2<A3) r Eturn A2; else return A3; }}public static void Main (string[] args) {//TODO auto-generated method stub}}

This problem tells us that the middle state in dynamic programming is not necessarily one-dimensional but may be two-dimensional

Leetcode Edit Distance

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.