C++
substring
algorithm
coding problem
string manipulation

Longest substring that occurs at least twice C question

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In the realm of string manipulation problems, one intriguing challenge is identifying the longest substring of a given string that occurs at least twice. Such a problem is popular in coding interviews and competitive programming, particularly in languages like C++. This problem is not only a test of one's command over string processing but also involves efficient algorithm design.

Problem Definition

Given a string `s`, the task is to find the longest substring that appears at least twice in `s`. The occurrences of the substring can overlap. For instance, if the input string is `"banana"`, the longest substring that appears at least twice is `"ana"`.

Approaches to Solve the Problem

Naive Approach

A naive solution would involve generating all possible substrings and checking if each occurs more than once. Though straightforward, this method is inefficient due to its high time complexity.

Time Complexity: O(n3)O(n^3) (where nn is the length of the string).

Optimized Approach with Suffix Array and LCP

A more efficient technique involves the use of Suffix Arrays and the Longest Common Prefix (LCP) Array:

  1. Suffix Array: This is an array of integers representing the starting indices of suffixes of a string arranged in lexicographical order.
  2. LCP Array: This array indicates the length of the longest common prefix between each pair of consecutive suffixes in the suffix array.

Algorithm Steps:

  1. Construct the suffix array for the string `s`.
  2. Build the LCP array from the suffix array.
  3. The maximum value in the LCP array is the length of the longest substring that occurs at least twice.

This approach leverages the lexicographical arrangement of suffixes to efficiently determine the longest repeated substring.

Time Complexity: Approximately O(nlogn)O(n \log n) due to the construction of the suffix array and LCP array.

Example

Consider the string `s = "banana"`:

  • Suffix Array: `[5, 3, 1, 0, 4, 2]` corresponding to `["a", "ana", "anana", "banana", "na", "nana"]`
  • LCP Array: `[0, 1, 3, 0, 2, 1]`

The value `3` in the LCP array indicates that `"ana"` is the longest repeated substring.

Implementation in C++


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.