ArticleslgStudy

science

Longest common substring

Longest common substring is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Longest common substring rather than just read about it. In short: 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.

Longest common substring — main illustration
Longest common substring — illustration

Key takeaways

  • Longest common substring belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Longest common substring to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Longest common substring from memory before moving on to harder problems.

Reference excerpt

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.

Illustrations

Longest common substring: Generalized suffix tree for the strings "ABAB", "BABA" and "ABBA", numbered 0, 1 and 2.
Generalized suffix tree for the strings "ABAB", "BABA" and "ABBA", numbered 0, 1 and 2.

Worked examples

Example 1 — a first encounter with Longest common substring

Start with the simplest possible case. Write down what Longest common substring claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Longest common substring before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Longest common substring ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Longest common substring

In research
Longest common substring appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Longest common substring in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Longest common substring is common in secondary-school and first-year university syllabi. It links to neighbouring topics Dynamic programming, Problems on strings, so understanding it makes those chapters shorter.
In everyday life
Look for Longest common substring outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Longest common substring in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Longest common substring means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Longest common substring out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Longest common substring in simple terms?

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.

Why does Longest common substring matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Longest common substring?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Longest common substring.

Tags

  • Dynamic programming
  • Problems on strings

Keep exploring