Skip to the code
cf.
+50 XP

#632 Count Distinct Snippets

hard

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.

Example 1

Input: text = "aba"
Output: 5

a, b, ab, ba, aba; the second a is a repeat.

Example 2

Input: text = "aaa"
Output: 3

Only a, aa, and aaa.

Constraints

  • 0 <= text.length <= 2 * 10^4
  • text contains lowercase letters.
  • The answer fits in a JavaScript number.
Saved in this browser

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).