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 "It was the best of times", but not a substring.
Prefixes and suffixes are special cases of substrings. A prefix of a string
S
{\displaystyle S}
is a substring of
S
{\displaystyle S}
that occurs at the beginning of
S
{\displaystyle S}
; likewise, a suffix of a string
S
{\displaystyle S}
is a substring that occurs at the end of
S
{\displaystyle S}
.
The substrings of the string "apple" would be:
"a", "ap", "app", "appl", "apple",
"p", "pp", "ppl", "pple",
"pl", "ple",
"l", "le"
"e", ""
(note the empty string at the end).
Substring
A string
u
{\displaystyle u}
is a substring (or factor) of a string
t
{\displaystyle t}
if there exists two strings
p
{\displaystyle p}
and
s
{\displaystyle s}
such that
t
=
p
u
s
{\displaystyle t=pus}
. In particular, the empty string is a substring of every string.
Example: The string
u
=
ana
{\displaystyle u={\texttt {ana}}}
is equal to substrings (and subsequences) of
t
=
banana
{\displaystyle t={\texttt {banana}}}
at two different offsets:
banana
|||||
ana||
|||
ana
The first occurrence is obtained with
p
=
b
{\displaystyle p={\texttt {b}}}
and
s
=
na
{\displaystyle s={\texttt {na}}}
, while the second occurrence is obtained with
p
=
ban
{\displaystyle p={\texttt {ban}}}
and
s
{\displaystyle s}
being the empty string.
A substring of a string is a prefix of a suffix of the string, and equivalently a suffix of a prefix; for example, nan is a prefix of nana, which is in turn a suffix of banana. If
u
{\displaystyle u}
is a substring of
t
{\displaystyle t}
, it is also a subsequence, which is a more general concept. The occurrences of a given pattern in a given string can be found with a string searching algorithm. Finding the longest string which is equal to a substring of two or more strings is known as the longest common substring problem.
In the mathematical literature, substrings are also called subwords (in America) or factors (in Europe).
Prefix
A string
p
{\displaystyle p}
is a prefix of a string
t
{\displaystyle t}
if there exists a string
s
{\displaystyle s}
such that
t
=
p
s
{\displaystyle t=ps}
. A proper prefix of a string is not equal to the string itself; some sources in addition restrict a proper prefix to be non-empty. A prefix can be seen as a special case of a substring.
Example: The string ban is equal to a prefix (and substring and subsequence) of the string banana:
banana
|||
ban
The square subset symbol is sometimes used to indicate a prefix, so that
p
⊑
t
{\displaystyle p\sqsubseteq t}
denotes that
p
{\displaystyle p}
is a prefix of
t
{\displaystyle t}
. This defines a binary relation on strings, called the prefix relation, which is a particular kind of prefix order.
Suffix
A string
s
{\displaystyle s}
is a suffix of a string
t
{\displaystyle t}
if there exists a string
p
{\displaystyle p}
such that
t
=
p
s
{\displaystyle t=ps}
. A proper suffix of a string is not equal to the string itself. A more restricted interpretation is that it is also not empty.[1] A suffix can be seen as a special case of a substring.
Example: The string nana is equal to a suffix (and substring and subsequence) of the string banana:
banana
||||
nana
A suffix tree for a string is a trie data structure that represents all of its suffixes. Suffix trees have large numbers of applications in string algorithms. The suffix array is a simplified version of this data structure that lists the start positions of the suffixes in alphabetically sorted order; it has many of the same applications.
Border
A border is suffix and prefix of the same string, e.g. "
bab
{\displaystyle {\texttt {bab}}}
" is a border of "
babab
{\displaystyle {\texttt {babab}}}
" (and also of "
baboon
eating
a
kebab
{\displaystyle {\texttt {baboon}}\,\,{\texttt {eating}}\,\,{\texttt {a}}\,\,{\texttt {kebab}}}
").
Superstring
A superstring of a finite set
P
{\displaystyle P}
of strings is a single string that contains every string in
P
{\displaystyle P}
as a substring. For example,
bcclabccefab
{\displaystyle {\texttt {bcclabccefab}}}
is a superstring of
P
=
{
abcc
,
efab
,
bccla
}
{\displaystyle P=\{{\texttt {abcc}},{\texttt {efab}},{\texttt {bccla}}\}}
, and
efabccla
{\displaystyle {\texttt {efabccla}}}
is a shorter one. Concatenating all members of
P
{\displaystyle P}
, in arbitrary order, always obtains a trivial superstring of
P
{\displaystyle P}
. Finding superstrings whose length is as small as possible is a more interesting problem, see Shortest common superstring.
A string that contains every possible permutation of a specified character set is called a superpermutation.
See also
Substring index
Suffix automaton
References
