Find all possible substring in fastest way
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
Generating all substrings of a string is inherently expensive because there are n*(n+1)/2 substrings for length n. That means no algorithm can list all substrings in less than quadratic output size. So the "fastest way" depends on what you actually need:
- enumerate all substrings,
- count unique substrings,
- search/query substrings efficiently.
If you only need counts or queries, advanced structures (suffix array/tree/automaton) can avoid explicit materialization.
Core Sections
1. Optimal enumeration baseline (output-sensitive)
For raw enumeration, nested loops are the direct method.
Complexity:
- number of substrings:
O(n^2) - total copied characters in naive slicing: up to
O(n^3)in some languages/runtime contexts.
In Python, substring slicing creates new strings, so memory/time can be high for large n.
2. Avoid materializing when not needed
If downstream logic only checks predicates or streams results, keep generator style and process on the fly.
Avoid list(all_substrings(...)) for large strings.
For deduplicated unique substrings, a set works but can explode memory:
3. Faster specialized structures for query/count use cases
Suffix array/tree/automaton can answer many substring problems without generating every substring explicitly.
Example: counting distinct substrings with suffix automaton is near linear in string length.
Conceptual direction (not full implementation):
- build suffix automaton in
O(n), - distinct substring count from state lengths.
For repeated substring search across one large text, suffix array or suffix tree provides much faster query performance than brute-force substring generation.
4. Practical performance tips
- Clarify exact requirement before coding.
- Use generators for streaming.
- For huge strings, write in compiled language or use C-accelerated libraries.
- Benchmark realistic input sizes.
This quickly shows quadratic growth in action.
Common Pitfalls
- Asking for "fastest" while still requiring full explicit list of all substrings.
- Materializing all substrings in memory and hitting RAM limits on large inputs.
- Ignoring string-copy cost and assuming two-loop enumeration is only
O(n^2)in practice. - Using brute force when requirement is actually query/count, where suffix structures are superior.
- Benchmarking with tiny strings and extrapolating incorrectly to production sizes.
Summary
You cannot beat quadratic output size when enumerating every substring. The best practical approach is to stream substrings lazily and avoid materialization unless necessary. If your real goal is counting or searching, use suffix-based data structures to achieve dramatically better performance characteristics.
For very large text processing tasks, representation choices matter. Instead of slicing strings repeatedly, some systems operate on index pairs (start, end) and only materialize substrings when required. This can significantly reduce memory pressure when downstream processing can work with offsets. It also enables compatibility with memory-mapped files and streaming parsers where copying substrings is expensive.
If duplicates are important, consider rolling hash or suffix-based indexes to avoid repeated comparisons of long substrings. These approaches increase implementation complexity, so they are most useful when performance constraints are strict. Benchmarking with realistic workloads is essential before committing to advanced structures.
For interview and teaching contexts, explicitly separating "enumeration" and "query" problems avoids confusion and leads to better algorithm choices.
Always align algorithm choice with concrete output requirements.
Related reading
- Find all subsets of length k in an array
- find all subsets that sum to a particular value
- Find all substrings that are palindromes
- Find all the paths forming simple cycles on an undirected graph
- Find common elements from two very large Arrays
- Find duplicate element in array in time On
- Find an algorithm to balance this game
- Find an algorithm to win this battle against crime!

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.