This is a premium problem. We're working on making it available for free soon.
Use these hints if you're stuck. Try solving on your own first.
Calculate the prefix hashing array for s.
Use the prefix hashing array to calculate the hashing value of each substring.
Compare the hashing values to determine the unique substrings.
There could be collisions if you use hashing, what about double hashing.
Solutions for this premium problem will be available for free soon.
Browse Free ProblemsWatch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, rolling hash techniques such as Rabin–Karp can be used to hash substrings efficiently. By storing computed hashes in a set, you can track unique substrings while avoiding repeated string comparisons. This approach is conceptually simpler but may still have O(n^2) complexity.
Yes, variations of this problem frequently appear in technical interviews, especially in companies that test advanced string algorithms. Interviewers may expect candidates to discuss Trie, suffix array, or hashing-based strategies and analyze their complexity.
An efficient approach uses a suffix array combined with an LCP (Longest Common Prefix) array. By sorting suffixes and subtracting shared prefixes, you can count how many new substrings each suffix contributes. This reduces redundant comparisons compared to brute-force substring generation.
A Trie is a popular data structure for this problem because it naturally stores prefixes and avoids counting duplicate substrings. Each new node created during insertion represents a unique substring. However, it can require significant memory for large strings.