In computer science, a longest common substring of two or more strings is a longest string that is a substring of all of them. There may be more than one longest common substring. Applications include data deduplication and plagiarism detection. 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.
Examples
The picture shows two strings where the problem has multiple solutions. Although the substring occurrences always overlap, it is impossible to obtain a longer common substring by "uniting" them. The strings "ABABC", "BABCA" and "ABCBA" have only one longest common substring, viz. "ABC" of length 3. Other common substrings are "A", "AB", "B", "BA", "BC" and "C".
ABABC ||| BABCA ||| ABCBA
Problem definition Given two strings, S {\displaystyle S} of length m {\displaystyle m} and T {\displaystyle T} of length n {\displaystyle n} , find a longest string which is substring of both S {\displaystyle S} and T {\displaystyle T} . A generalization is the k-common substring problem. Given the set of strings S = { S 1 , … , S K } {\displaystyle S=\{S_{1},\ldots ,S_{K}\}} , where | S i | = n i {\displaystyle |S_{i}|=n_{i}} and ∑ n i = N {\textstyle \sum n_{i}=N} . Find for each 2 ≤ k ≤ K {\displaystyle 2\leq k\leq K} , a longest string which occurs as substring of at least k {\displaystyle k} strings.
Algorithms One can find the lengths and starting positions of the longest common substrings of S {\displaystyle S} and T {\displaystyle T} in Θ {\displaystyle \Theta }
( n + m ) {\displaystyle (n+m)} time with the help of a generalized suffix tree. A faster algorithm can be achieved in the word RAM model of computation if the size σ {\displaystyle \sigma } of the input alphabet is in 2 o ( log ( n + m ) ) {\displaystyle 2^{o\left({\sqrt {\log(n+m)}}\right)}} . In particular, this algorithm runs in O ( ( n + m ) log σ / log ( n + m ) ) {\textstyle O\left((n+m)\log \sigma /{\sqrt {\log(n+m)}}\right)} time using O ( ( n + m ) log σ / log ( n + m ) ) {\displaystyle O\left((n+m)\log \sigma /\log(n+m)\right)} space. Solving the problem by dynamic programming costs Θ ( n m ) {\displaystyle \Theta (nm)} . The solutions to the generalized problem take Θ ( n 1 + ⋯ + n K ) {\displaystyle \Theta (n_{1}+\cdots +n_{K})} space and Θ ( n 1 ⋯ n K ) {\displaystyle \Theta (n_{1}\cdots n_{K})} time with dynamic programming and take Θ ( n 1 + ⋯ + n K ) {\displaystyle \Theta (n_{1}+\cdots +n_{K})} time with a generalized suffix tree.
Suffix tree
… excerpt ends here. Continue reading the full article.


