Topic
the longest common child string
Gives two strings, finds the longest common substring, and returns its length.
Sample Example
Give a="ABCD", b="CBCE", return 2
Note
The characters of the substring should appear consecutively in the original string, which differs from the subsequence.
Solving
Attention:
Subsequence: This sequence is not contiguous in the original string, but is spaced, such as: ABCDE and ambmcmdmem the longest common subsequence is ADCDE
SUBSTRING: The substring must exist consecutively in the original string. such as: ABCDEF and sssabcdoooo the longest common substring is ABCD
Refer to the link, the explanation is very detailed
According to the substring definition, brute force
Public classSolution {/** * @paramA, b:two string. * @return: The length of the longest common substring. */ Public intlongestcommonsubstring (String A, String B) {//Write your code here if(A = =NULL|| B = =NULL|| A.length () ==0 | | B.length () ==0) return0; intLenA =a.length (); intLenB =b.length (); intLongest =-1; for(inti=0; I <lena; i++){ for(intj=0; J <lenb; J + +){ intm =i; intn =J; intSublongest = 0; while(M<lena && N <LenB) { CharCH1 =A.charat (m); CharCH2 =B.charat (n); if(Ch1 = =CH2) {m++; N++; Sublongest+ = 1; }Else{ Break; }} longest=Math.max (longest,sublongest); } } returnlongest; }}
Java Code
Time complexity O (n2m2)
The idea of the solution is as stated above, with each character in the string as the end of the substring, determining the longest length of the same character of the substring starting with this. Actually from the surface, the complexity of this algorithm should be only O (N2) because the algorithm compares each word Fu Ducheng to each other, but the key problem is that comparing the efficiency of two strings is not O (1), which also leads to the actual time complexity should be met O (N2) and O (N3).
The first method of dynamic programming is given in the above blog.
Definition array c[lena+1][lenb+1] c[i][j] means string A to a[i-1] end B to b[j-1] The length of the maximum same substring
When A[i-1] ==b[j-1] [c[i][j] = C[i-1][j-1] + 1 Understanding the definition of a substring is obvious, "continuous string"
When A[i-1] ==b[j-1] c[i][j] = 0
The maximum value in the array is the answer.
Public classSolution {/** * @paramA, b:two string. * @return: The length of the longest common substring. */ Public intlongestcommonsubstring (String A, String B) {//Write your code here//String A = "cpoe.com code"; //String B = "ccht.com code"; if(A = =NULL|| B = =NULL|| A.length () ==0 | | B.length () ==0) return0; intLenA =a.length (); intLenB =b.length (); int[] C =New int[LenA + 1] [LenB + 1]; intLongest =-1; for(inti=1;i<= lena;i++){ for(intj= 1;j<= lenb;j++){ CharCH1 = A.charat (i-1); CharCH2 = B.charat (j-1); if(Ch1 = =CH2) {C[i][j]= C[i-1][j-1] + 1; }Else{C[i][j]= 0; } Longest=Math.max (c[i][j],longest); } } returnlongest; }}
Java Code
In the topic of finding the longest common subsequence, I have considered using arrays for solving, but the good solution is not directly using the simple array I have defined.
For this topic, the string array is represented as: the same character is 1
You will find: The substring must be on the diagonal, the continuous diagonal 1 is the longest substring, the starting position of the substring is the first 1 on the left, the end position is the bottom right of the last one, the violent method is the number of diagonal 1 of the beginning of each point, the maximum value is the answer, This method is in fact a means of the above violent method.
Public classSolution {/** * @paramA, b:two string. * @return: The length of the longest common substring. */ Public intlongestcommonsubstring (String A, String B) {//Write your code here if(A = =NULL|| B = =NULL|| A.length () ==0 | | B.length () ==0) return0; intLenA =a.length (); intLenB =b.length (); int[] C =New int[LenA][lenb]; intLongest =-1; for(inti=0;i<lena;i++){ for(intj= 0;j< lenb;j++){ CharCH1 =A.charat (i); CharCH2 =B.charat (j); if(Ch1 = =CH2) {C[i][j]= 1; }Else{C[i][j]= 0; } } } for(intI =0;i< lena;i++){ for(intj=0;j<lenb;j++){ intm =i; intn =J; intSublongest = 0; while(M<lena && N <LenB) { if(C[m][n] ==1) {sublongest+=1; M++; N++; }Else{ Break; }} longest=Math.max (longest,sublongest); } } returnlongest; }}
Java Code
The above dynamic programming solution is to find the number of groups at the same time the diagonal 1, the above the separate calculation is easier to understand, more easily think of the solution of dynamic programming.
Above blog also want other method, wait to update ...
Lintcode Medium title: Longest common substring longest common substring