Longest common substring
In computer science, a longest common substring of two or more strings is a longest string that is a substring of all of them.[src] There may be more than one longest common substring.[src] Applications include data deduplication and plagiarism detection.[src] Unlike the longest common subsequence problem, which finds insertions or deletions within the common text, the longest common substring problem seeks a contiguous substring shared by both texts.[src]
In computer science, a longest common substring of two or more strings is a longest string that is a substring of all of them.[src] There may be more than one longest common substring.[src] Applications include data deduplication and plagiarism detection.[src] Unlike the longest common subsequence problem, which finds insertions or deletions within the common text, the longest common substring problem seeks a contiguous substring shared by both texts.[src] ==Examples== The picture shows two strings where the problem has multiple solutions.[src] Although the substring occurrences always overlap, it is impossible to obtain a longer common substring by "uniting" them.[src] The strings "ABABC", "BABCA" and "ABCBA" have only one longest common substring, viz.[src] "ABC" of length 3.[src] Other common substrings are "A", "AB", "B", "BA", "BC" and "C".[src] ABABC ||| BABCA ||| ABCBA ==Problem definition== Given two strings, S of length m and T of length n , find a longest string which is substring of both S and T .[src] A generalization is the k-common substring problem.[src] Given the set of strings S = \{S_1, \ldots, S_K\} , where |S_i|=n_i and \sum n_i = N .[src] Find for each 2 \leq k \leq K , a longest string which occurs as substring of at least k strings.[src] ==Algorithms== One can find the lengths and starting positions of the longest common substrings of S and T in \Theta (n+m) time with the help of a generalized suffix tree.[src] A faster algorithm can be achieved in the word RAM model of computation if the size \sigma of the input alphabet is in 2^{o \left( \sqrt{\log(n+m)} \right) } .[src] In particular, this algorithm runs in O\left( (n+m) \log \sigma/\sqrt{\log (n+m)} \right) time using O\left((n+m)\log\sigma/\log (n+m) \right) space.[src] Solving the problem by dynamic programming costs \Theta(nm) .[src] The solutions to the generalized problem take \Theta(n_1 + \cdots + n_K) space and \Theta(n_1 \cdots n_K) time with dynamic programming and take \Theta(n_1 + \cdots + n_K) time with a generalized suffix tree.[src] ===Suffix tree=== for the strings "ABAB", "BABA" and "ABBA", numbered 0, 1 and 2.]] The longest common substrings of a set of strings can be found by building a generalized suffix tree for the strings, and then finding the deepest internal nodes which have leaf nodes from all the strings in the subtree below it.[src] The figure on the right is the suffix tree for the strings "ABAB", "BABA" and "ABBA", padded with unique string terminators, to become "ABAB$0", "BABA$1" and "ABBA$2".[src] The nodes representing "A", "B", "AB" and "BA" all have descendant leaves from all of the strings, numbered 0, 1 and 2.[src] Building the suffix tree takes \Theta(N) time (if the size of the alphabet is constant).[src] If the tree is traversed from the bottom up with a bit vector telling which strings are seen below each node, the k-common substring problem can be solved in \Theta(NK) time.[src] If the suffix tree is prepared for constant time lowest common ancestor retrieval, it can be solved in \Theta(N) time.[src] ===Dynamic programming=== The following pseudocode finds the set of longest common substrings between two strings with dynamic programming: function LongestCommonSubstring(S[1..r], T[1..n]) L := array(1..r, 1..n) z := 0 # length of the longest common substring found so far ret := {} for i := 1..r for j := 1..n if S[i] = T[j] if i = 1 or j = 1 L[i, j] := 1 else L[i, j] := L[i − 1, j − 1] + 1 if L[i, j] > z z := L[i, j] ret := {S[(i − z + 1)..i]} else if L[i, j] = z ret := ret ∪ {S[(i − z + 1)..i]} else L[i, j] := 0 return ret This algorithm runs in O(n r) time.[src] The array L stores the length of the longest common suffix of the prefixes S[1..i] and T[1..j] which end at position i and j , respectively.[src] The variable z is used to hold the length of the longest common substring found so far.[src] The set ret is used to hold the set of strings which are of length z .[src] The set ret can be saved efficiently by just storing the index i , which is the last character of the longest common substring (of size z) instead of S[(i-z+1)..i] .[src] Thus all the longest common substrings would be, for each i in ret , S[(ret[i]-z)..(ret[i])] .[src] The following tricks can be used to reduce the memory usage of an implementation: Keep only the last and current row of the DP table to save memory ( O(\min(r, n)) instead of O(n r) ) The last and current row can be stored on the same 1D array by traversing the inner loop backwards Store only non-zero values in the rows.[src] This can be done using hash-tables instead of arrays.[src] This is useful for large alphabets.[src] ==See also== Longest palindromic substring n-gram, all the possible substrings of length n that are contained in a string ==References== ==External links== Dictionary of Algorithms and Data Structures: longest common substring Perl/XS implementation of the dynamic programming algorithm Perl/XS implementation of the suffix tree algorithm Dynamic programming implementations in various languages on wikibooks working AS3 implementation of the dynamic programming algorithm Suffix Tree based C implementation of Longest common substring for two strings[src]
38 sentences · 38 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Longest common substring." encyclopegia.org, 2026, https://encyclopegia.org/wiki/longest-common-substring. Accessed 30 November 2026.
Chicago
"Longest common substring." encyclopegia.org. Accessed November 30, 2026. https://encyclopegia.org/wiki/longest-common-substring.
APA
encyclopegia.org. (2026). Longest common substring. Retrieved November 30, 2026, from https://encyclopegia.org/wiki/longest-common-substring
Accessed November 30, 2026 · CC BY-SA 4.0 · citation page · JSON