`

Longest Common Substring

 
阅读更多

Given two strings ‘X’ and ‘Y’, find the length of the longest common substring. For example, if the given strings are “GeeksforGeeks” and “GeeksQuiz”, the output should be 5 as longest common substring is “Geeks”

 

Let m and n be the lengths of first and second strings respectively.

A simple solution is to one by one consider all substrings of first string and for every substring check if it is a substring in second string. Keep track of the maximum length substring. There will be O(m^2) substrings and we can find whether a string is subsring on another string in O(n) time (See this). So overall time complexity of this method would be O(n * m2)

Dynamic Programming can be used to find the longest common substring in O(m*n) time. The idea is to find length of the longest common suffix for all substrings of both strings and store these lengths in a table.

The longest common suffix has following optimal substructure property
   LCSuff(X, Y, m, n) = LCSuff(X, Y, m-1, n-1) + 1 if X[m-1] = Y[n-1]
                        0  Otherwise (if X[m-1] != Y[n-1])

The maximum length Longest Common Suffix is the longest common substring.
   LCSubStr(X, Y, m, n)  = Max(LCSuff(X, Y, i, j)) where 1 <= i <= m
                                                     and 1 <= j <= n

 

Implementation:

 

public String getLongestCommonString(String a, String b) {
	int m = a.length(), n = b.length();
	int[][] f = new int[m+1][n+1];
	int max = 0;
	String lcs = "";
	for(int i=1; i<=m; i++) {
		for(int j=1; j<=n; j++) {
			if(a.charAt(i-1) == b.charAt(j-1)) {
				f[i][j] = f[i-1][j-1]+1;
				if(f[i][j] > max) {
					max = f[i][j];
					lcs = a.substring(i-max, i);
				}
			}
		}
	}
	return lcs;
}

 

 

Time Complexity: O(m*n)
Auxiliary Space: O(m*n)

 

The longest substring can also be solved in O(n+m) time using Suffix Tree

 

References: 

http://en.wikipedia.org/wiki/Longest_common_substring_problem

http://www.geeksforgeeks.org/longest-common-substring/

http://www.geeksforgeeks.org/suffix-tree-application-5-longest-common-substring-2/

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics