Duplicate substring searching
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Duplicate substring searching asks whether a string contains the same contiguous sequence of characters more than once. Sometimes the goal is to find any repeated substring, and sometimes the goal is to find the longest repeated substring, which is a harder problem and usually changes the algorithm choice.
Start with the Naive Approach
The simplest idea is to enumerate substrings and remember what you have seen:
This is easy to understand, but it is expensive. There are O(n^2) substrings, and creating or comparing them repeatedly can push the practical cost much higher.
Clarify the Exact Question
Before optimizing, decide what you need:
- any duplicate substring
- all duplicate substrings
- the longest duplicate substring
- duplicate substrings of at least a certain length
Those are related problems, but they are not the same. A quick set-based solution may be fine for "does any duplicate exist" on short strings, while "find the longest duplicate substring" usually needs a more specialized approach.
Use Binary Search Plus Rolling Hash for the Longest Case
For large inputs, a common technique is binary search on substring length combined with a rolling hash. The binary search asks: "does a duplicate substring of length k exist?" If yes, try longer; if not, try shorter.
Here is a simplified version:
This version still slices strings, so it is not the most optimized possible implementation, but it shows the core strategy clearly. In production, rolling hashes or suffix-array-style approaches reduce repeated substring comparisons.
Know the Tradeoff Between Simplicity and Scale
For interview problems or small inputs, the simpler solution is often preferable because it is easier to reason about and verify. For very large strings, repeated slicing becomes expensive, and data structures such as suffix arrays, suffix automata, or Rabin-Karp style rolling hashes become more attractive.
That is why the right answer depends heavily on the expected input size and on whether you need exact matches, counts, or only one representative substring.
Common Pitfalls
The biggest mistake is not defining the problem precisely. "Find duplicate substrings" can mean several different things, and each version suggests a different implementation.
Another issue is underestimating the number of substrings. Even moderate string lengths can produce a large amount of work for naive algorithms.
People also forget about overlapping duplicates. In "banana", the substring "ana" appears more than once with overlap, and many formulations of the problem count that as a valid duplicate.
Finally, if you switch to hashing for speed, remember that careless hashing can introduce collisions. A robust solution either uses a collision-resistant strategy or verifies candidate matches explicitly.
Summary
- Duplicate substring searching can mean "any", "all", or "longest" repeated substrings.
- A naive set-based approach is easy to write but grows expensive quickly.
- Binary search plus substring existence checks is a common strategy for longest-duplicate problems.
- Large inputs may require rolling hashes or suffix-based data structures.
- Define whether overlapping duplicates count before choosing an algorithm.
Related reading
- Dynamic addition of queues to a rabbit listener at runtime
- Dynamic Array with O1 removal of any element
- Dynamic Nested Loop
- Dynamic Programming Algorithm for Segmented Least Squares
- dynamic programming and the use of matrices
- Dynamic programming aspect in Kadane's algorithm
- Dynamic programming Code Wars twice linear algorithm times out
- Dynamic Programming Coin Change Problems

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 courseTrack 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.