In
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, the longest common substring problem is to find a longest
string that is a
substring
In formal language theory and computer science, a substring is a contiguous sequence of characters within a string. For instance, "''the best of''" is a substring of "''It was the best of times''". In contrast, "''Itwastimes''" is a subsequence ...
of two or more strings. The problem may have multiple solutions.
Applications include
data deduplication
In computing, data deduplication is a technique for eliminating duplicate copies of repeating data. Successful implementation of the technique can improve storage utilization, which may in turn lower capital expenditure by reducing the overall amou ...
and
plagiarism detection
Plagiarism detection or content similarity detection is the process of locating instances of plagiarism or copyright infringement within a work or document. The widespread use of computers and the advent of the Internet have made it easier to pla ...
.
Examples
The picture shows two strings where the problem has multiple solutions. Although the substring occurrences always overlap, no longer common substring can be obtained 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,
of length
and
of length
, find a longest string which is substring of both
and
.
A generalization is the k-common substring problem. Given the set of strings
, where
and
. Find for each
, a longest string which occurs as substring of at least
strings.
Algorithms
One can find the lengths and starting positions of the longest common substrings of
and
in
time with the help of a
generalized suffix tree
In computer science, a generalized suffix tree is a suffix tree for a set of strings. Given the set of strings D=S_1,S_2,\dots,S_d of total length n, it is a Patricia tree containing all n suffixes of the strings. It is mostly used in bioinforma ...
. A faster algorithm can be achieved in the
word RAM In theoretical computer science, the word RAM (word random-access machine) model is a model of computation in which a random-access machine does bitwise operations on a word of bits. Michael Fredman and Dan Willard created it in 1990 to simulate pr ...
model of computation if the size
of the input alphabet is in
. In particular, this algorithm runs in
time using
space.
[ Here: Theorem 1, p.30:2.] Solving the problem by
dynamic programming
Dynamic programming is both a mathematical optimization method and a computer programming method. The method was developed by Richard Bellman in the 1950s and has found applications in numerous fields, from aerospace engineering to economics.
I ...
costs
. The solutions to the generalized problem take
space and
·...·
time with
dynamic programming
Dynamic programming is both a mathematical optimization method and a computer programming method. The method was developed by Richard Bellman in the 1950s and has found applications in numerous fields, from aerospace engineering to economics.
I ...
and take
time with a
generalized suffix tree
In computer science, a generalized suffix tree is a suffix tree for a set of strings. Given the set of strings D=S_1,S_2,\dots,S_d of total length n, it is a Patricia tree containing all n suffixes of the strings. It is mostly used in bioinforma ...
.
Suffix tree
The longest common substrings of a set of strings can be found by building a
generalized suffix tree
In computer science, a generalized suffix tree is a suffix tree for a set of strings. Given the set of strings D=S_1,S_2,\dots,S_d of total length n, it is a Patricia tree containing all n suffixes of the strings. It is mostly used in bioinforma ...
for the strings, and then finding the deepest internal nodes which have leaf nodes from all the strings in the subtree below it. 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". The nodes representing "A", "B", "AB" and "BA" all have descendant leaves from all of the strings, numbered 0, 1 and 2.
Building the suffix tree takes
time (if the size of the alphabet is constant). 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
time. If the suffix tree is prepared for constant time
lowest common ancestor
In graph theory and computer science, the lowest common ancestor (LCA) (also called least common ancestor) of two nodes and in a Tree (graph theory), tree or directed acyclic graph (DAG) is the lowest (i.e. deepest) node that has both and a ...
retrieval, it can be solved in
time.
Dynamic programming
The following pseudocode finds the set of longest common substrings between two strings with dynamic programming:
function LCSubstr(S
..r T
..n
L := array(1..r, 1..n)
z := 0
ret :=
for i := 1..r
for j := 1..n
if S
= T
if i = 1 or j = 1
L
, j
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline (t ...
:= 1
else
L
, j
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline (t ...
:= L
− 1, j − 1+ 1
if L
, j
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline (t ...
> z
z := L
, j
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline (t ...
ret :=
else if L
, j
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline (t ...
= z
ret := ret ∪
else
L
, j
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline (t ...
:= 0
return ret
This algorithm runs in
time. The array
L
stores the longest common substring of the prefixes
S ..i/code> and T ..j/code> which ''end at position'' S /code>, T /code>, resp. The variable z
is used to hold the length of the longest common substring found so far. The set ret
is used to hold the set of strings which are of length z
. 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 -z+1..i/code>. Thus all the longest common substrings would be, for each i in ret
, S ret[iz)..(ret[i.html"_;"title=".html"_;"title="ret[i">ret[iz)..(ret[i">.html"_;"title="ret[i">ret[iz)..(ret[i.html" ;"title="">ret[iz)..(ret[i.html" ;"title=".html" ;"title="ret[i">ret[iz)..(ret[i">.html" ;"title="ret[i">ret[iz)..(ret[i">">ret[iz)..(ret[i.html" ;"title=".html" ;"title="ret[i">ret[iz)..(ret[i">.html" ;"title="ret[i">ret[iz)..(ret[i/code>.
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 ( instead of )
* Store only non-zero values in the rows. This can be done using hash-tables instead of arrays. This is useful for large alphabets.
See also
* Longest palindromic substring
* n-gram, ''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
{{Strings , state=collapsed
Problems on strings
Dynamic programming
Articles with example pseudocode