given two strings str1 and str2, returns the longest common substring of two strings. For example: str1= "1AB2345CD", str2= "12345EF", common substring is "2345"
The difference between the longest common substring and the longest common subsequence is that the substrings are contiguous, and the subsequence is discontinuous.
The first step is to generate a dynamic planning table. Generates a matrix DP of size m*n. DP[I][J] means how long a common substring can be as long as the last character of a common substring must be str1[i] and str2[j]. For example, the meaning of str1= "a1234b", str2= "CD1234", dp[3][4] is how long the common substring can be if it is necessary to treat str1[3] and str2[4 as the last character of the common substring. So dp[3][4]=3. If str1[i]! = str2[j], then dp[i][j]=0. The specific solution process is as follows:
1. Matrix DP The first column is dp[0..m-1][0]. If str1[i]==str2[0],dp[i][0]=1, otherwise dp[i][0]=0.
2. Matrix DP First line ibid.
3. From left to right, there are two possible scenarios from top to bottom:
1) if str1[i]! = str2[j], then dp[i][j]=0
2) if str1[i] = = Str2[j], then dp[i][j]=dp[i-1][j-1] + 1
The code to calculate the DP matrix is as follows:
1 Public int[] GETDP (Char[] str1,Char[] str2) {2 int[] DP =New int[str1.length][str2.length];3 for(inti = 0; i < str1.length; i++) {4 if(Str1[i] = = Str2[0])5Dp[i][0] = 1;6 }7 for(intj = 0; J < Str2.length; J + +) {8 if(Str1[0] = =Str2[j])9DP[0][J] = 1;Ten } One for(inti = 1; i < str1.length; i++) { A for(intj = 1; J < Str2.length; J + +) { - if(Str1[i] = =Str2[j]) { -DP[I][J] = dp[i-1][j-1] + 1; the } - } - } - returnDP; +}
It is very easy to get the longest common substring from the DP matrix. You just need to find the largest number in the DP matrix. The code is as follows.
1 Publicstring Lcst1 (String str1, String str2) {2 if(str1 = =NULL|| STR2 = =NULL|| Str1.equals ("") | | Str2.equals ("")) {3 return"";4 }5 Char[] Chs1 =Str1.tochararray ();6 Char[] Chs2 =Str2.tochararray ();7 int[] DP =GETDP (CHS1, CHS2);8 intEnd = 0;9 intMax = 0;Ten for(inti = 0; i < chs1.length; i++) { One for(intj = 0; J < Chs2.length; J + +) { A if(Dp[i][j] >max) { -End =i; -Max =Dp[i][j]; the } - } - } - returnStr1.substring (end-max+1, end+1); +}
The above algorithm requires a matrix of size m*n, but actually can be reduced to O (1), as shown in, each slash before the calculation to generate an integer variable Len,len represents the value of the upper left position, the initial len=0. The value of each position is calculated from the left-most position of the slash to the bottom right.
The code is implemented as follows:
1 Publicstring Lcst2 (String str1, String str2) {2 if(str1 = =NULL|| STR2 = =NULL|| Str1.equals ("") | | Str2.equals ("")) {3 return"";4 }5 Char[] Chs1 =Str1.tochararray ();6 Char[] Chs2 =Str2.tochararray ();7 introw = 0;//line starting with Slash8 intcol = chs2.length-1;//column starting with Slash9 intmax = 0;//Record Maximum lengthTen intend = 0;//when the maximum length is updated, the end position of the record substring One while(Row <chs1.length) { A inti =Row; - intj =Col; - intLen = 0; the //Traverse from (i, j) to the right - while(I < chs1.length && J <chs2.length) { - if(Chs1[i]! =Chs2[j]) { -Len = 0; +}Else { -len++; + } A //record the maximum and position of the ending character at if(Len >max) { -End =i; -Max =Len; - } -i++; -J + +; in } - if(Col > 0) {//the column with the slash start position moves to the left first tocol--; +}Else{//row moves down after the column is moved to the leftmost position -row++; the } * } $ returnStr1.substring (end-max+1, end+1);Panax Notoginseng}
Programmer Code Interview Guide left Chengyun
One day Algorithm puzzle (5)---longest common substring