"banana" is 3, since it only uses a, b and n.
You are given a string text. Take every non-empty substring, that is, every contiguous piece of the string; pieces with the same content at different positions count separately. Return the sum of their varieties.
Listing all substrings costs O(n²) or more; try to get away with a single pass.text='kayak' → 30
The one-letter substrings give 5. The two-letter ones "ka", "ay", "ya", "ak" give 2 each, 8 in total. The three-letter ones "kay", "aya", "yak" give 3 + 2 + 3 = 8. The four-letter ones "kaya" and "ayak" give 3 each, 6 in total. The whole string gives 3. The sum is 5 + 8 + 8 + 6 + 3 = 30.
text='mmm' → 6
There are six substrings, and each of them has exactly one distinct letter.
text='xyz' → 10
text consists of lowercase English letters onlytext='kayak'
30
Run checks the first 3 tests in your browser. Submit checks all 72 tests on the server, hidden ones included.