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 20,000 characters, so listing all substrings in a set is far too slow and memory-hungry.
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 <= 2 * 10^4text contains lowercase letters.Run tries your code on the examples. Submit runs every test, including hidden edge cases, 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.