An autocomplete service estimates how big a substring index for a text would be by counting its distinct non-empty substrings. Equal substrings at different positions count once.
Write countDistinct(text) that returns that number. Texts can be up to 100,000 characters, so making every substring is far too slow and needs far too much memory.
Input: text = "aba" Output: 5
a, b, ab, ba, aba; the second a is a repeat.
Input: text = "aaa" Output: 3
Only a, aa, and aaa.
0 <= text.length <= 10^5text contains lowercase letters.Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #632 Count Distinct Snippets. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.