Ctrl K

Pieces Without Repeats

Medium
You are given a string text of lowercase English letters. Count its contiguous substrings in which all letters are distinct, so no letter appears twice. Substrings with the same content at different positions count separately.
Example 1
text="moon" → 6

The pieces m, o, o, n, mo, on qualify, six in all. Anything like moo or longer repeats o.

Example 2
text="cab" → 6

All letters differ, so every piece works: 3 + 2 + 1 = 6.

Example 3
text="zzz" → 3
Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • text consists of lowercase English letters only
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Repeat-Free Stretch MediumTwo-Letter Stretch MediumRecurring Gene Windows MediumWindow with Few Kinds of Letters MediumEvery Letter at Least k Times Medium
loading Python…
Input
text="moon"
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 136 tests on the server, hidden ones included.