Tally Slices Whose Letters Are All Different
StringsAuto-graded · 17 test casesLast updated
Count every contiguous slice of a lowercase string in which no letter appears twice, using a linear-time sliding window.
Tally Slices Whose Letters Are All Different
You are given a string text made up of lowercase English letters. A slice is any non-empty run of consecutive characters from text. A slice is all-different if no letter occurs in it more than once.
Return how many all-different slices text has.
Slices are identified by their start and end positions, not by their contents. If two slices have the same characters but sit at different positions, count both.
Constraints
1 <= len(text) <= 100000textcontains only the lowercase letters'a'to'z'.- The answer can be large in general, so do not rely on small fixed-width integer types in languages that have them.
Edge cases
- A string where every character is the same has exactly
len(text)valid slices. - A string whose characters are all distinct with length
nhasn * (n + 1) // 2valid slices. - A one-character string has exactly one valid slice.
Examples
Example 1
Input: count_distinct_letter_slices('abc')
Output: 6
Example 2
Input: count_distinct_letter_slices('aba')
Output: 5
Hints
Hint 1
Enumerating all slices is far too slow for length 100000. Try grouping the slices by the position where they end.
Hint 2
For a fixed end position, if a slice is all-different then so is every shorter slice ending at the same position. So you only need to find the earliest valid start.
Hint 3
Remember the last position of each letter. When a new character arrives, move the earliest valid start to just after that letter's previous position, but never move it backward.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Counting slices one by one would take quadratic time or worse, so count them by their end position instead.
For a fixed end index end, let start be the smallest index such that text[start..end] has no repeated letter. Any slice ending at end that begins at or after start is a sub-window of a valid window, so it is valid too. Any slice beginning before start contains a repeat. That gives exactly end - start + 1 valid slices ending at end. Summing over all end positions gives the answer.
To maintain start as end advances, store the last index at which each of the 26 letters was seen:
- When reading
text[end], look up its previous indexp. - Set
start = max(start, p + 1). Ifplies inside the current window, the window must begin right after it. Ifpis older thanstart, the window is unaffected. Themaxstopsstartfrom moving backward. - Add
end - start + 1to the total. - Record
last_pos[letter] = end. This must happen after step 2, otherwise the character would conflict with itself.
Why it is correct
After processing each end, start is the minimal valid left boundary. A conflict at end can only involve an earlier copy of text[end]. If that copy is at p >= start, every window beginning at or before p has two copies, so the earliest valid start is p + 1. Otherwise nothing changes. Smaller starts were already invalid for end - 1, and a longer window contains that invalid one. Each slice is counted once, at its unique end position.
For example, "aba" gives contributions 1, 2, 2 for a total of 5.
Complexity
- Time: O(n), with constant work per character.
- Space: O(1), because the table has 26 entries.
Reference solution
def count_distinct_letter_slices(text: str) -> int:
last_pos = [-1] * 26
start = 0
total = 0
for end, ch in enumerate(text):
idx = ord(ch) - ord('a')
start = max(start, last_pos[idx] + 1)
total += end - start + 1
last_pos[idx] = end
return total