Ctrl K

Longest Repeat-Free Stretch

Medium
You are given a string text. Return the length of its longest contiguous piece in which no character appears more than once. The piece must be contiguous: you cannot skip characters in the middle. Spaces, digits and punctuation count as ordinary characters. For an empty string the answer is 0.
Example 1
text="xyzzyx" → 3

For instance xyz or zyx work, three distinct characters each. Any longer piece contains both letters z.

Example 2
text="a b!a" → 4

A space counts as a character too. The piece a b! has four distinct characters; adding the final a would repeat it.

Example 3
text="" → 0

An empty string has no characters, so the answer is 0.

Constraints
  • 0 ≤ length of text ≤ 5 · 10⁴
  • text consists of English letters, digits, punctuation symbols and spaces
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo-Letter Stretch MediumRecurring Gene Windows MediumWindow with Few Kinds of Letters MediumEvery Letter at Least k Times MediumOne-Color Run After Repaints Medium
loading Python…
Input
text="xyzzyx"
Expected
3

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