← DiffPush

Count Distinct Substrings

DiffPush Tier Tries · Problems O(n^2) · O(n^2)

The Rally Chant Census

A stadium anthem archive studies crowd chants. Analysts slice a chant recording into every contiguous syllable run and want to know how many unique runs exist — duplicates collapse into one. The empty run counts too. Two runs are the same only when their syllables match exactly.

Input: A string s of lowercase letters.

Output: Return the number of distinct substrings of s, including the empty substring.

Constraints

Examples

Example 1

Input: {"s":"abab"}
Output: 8
Distinct non-empty runs: a, b, ab, ba, aba, bab, abab — 7 of them; the canonical count adds the empty run for 8.

Example 2

Input: {"s":"aaa"}
Output: 4
a, aa, aaa plus the empty run.

Solve this in your browser →

Also on LeetCode ↗