Distinct Substrings
Here is an approach to find the number of distinct substrings of a given string:


The key steps are:
 Iterate through every start index i
 Generate substrings from i to all end indices j
 Add the substring to a set to avoid duplicates
This generates all substrings by sliding a window from size 1 up to the full string length. Using a set eliminates repeats.
The time complexity is O(N^2) since we loop from i = 0 to N and j = i+1 to N for each i.
Space complexity is O(N^2) as well since in worst case all substrings are distinct so we store N^2 strings.
To optimize, we could stop early if set size reaches N^2 since we know all substrings must be unique.
Here is an implementation in Java to find the number of distinct substrings:


And here is the implementation in C++:


The key aspects remain the same  generate all substrings by sliding a window, use a hash set/unordered_set to remove duplicates, and return the final set size.
Overcoming TLE
There are a few ways we can optimize the brute force approach to avoid exceeding time limits:
 Memoization
Store results of substrings in a hashmap. Before generating a substring, check if we’ve seen it before and skip readding to the set if found. This avoids recalculating duplicate substrings.
 Output just count, not full set
Rather than building the full set of substrings, just increment a counter whenever we generate a new substring. Return the final count, without keeping the full set in memory.
 Prefix/Suffix hashing
Use a hashmap to store prefixes and suffixes of the string. This allows quick lookup to rule out substrings that couldn’t possibly be unique or new based on matching existing prefixes/suffixes.
 Loop optimizations
Avoid nesting the entire inner loop over all substring lengths at each index. For example, increment inner end point j in chunks of size k to reduce number of iterations.
 Parallel processing
Process different parts of the string in parallel as threads/processes to divideandconquer the workload.
Combining techniques like memoization, better output representation, loop optimizations, and parallelism can help overcome the exponential time complexity in many substring problems.
We can optimize the substring problem further using Ukkonen’s algorithm:
The key ideas in Ukkonen’s algorithm are:
Build suffix tree for the string
Each suffix tree node represents a substring
Leaf nodes are suffixes, internal nodes are repeated subsequences
Traversing the suffix tree visits each substring exactly once
This allows generating all substrings in linear time O(n) by constructing and traversing the suffix tree, rather than the naive O(n^2) approach.
Here is an implementation:


By leveraging the suffix tree structure, we can greatly improve efficiency by avoiding generating duplicate and overlapping substrings in the naive approach.