Back to Practice
#0054
Longest Substring Without Repeating Characters
MediumDSA25 min20 XP
Problem
Given a string s, return the length of the longest contiguous substring that contains no repeated characters.
Why This Matters
This is one of the cleanest tests of whether you understand why the left pointer can safely jump forward instead of restarting the scan.
Function Signature
def longest_unique_substring(s):
Examples
Example 1
Inputs = "abcabcbb"
Output3
The longest valid substrings include "abc", "bca", and "cab".
Constraints
- Return 0 for an empty string.
- Treat uppercase and lowercase as different characters.
- Aim for O(n) time.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Classic repeat pattern
Input"abcabcbb"
Expected3
The best window length is 3.
Repeated same character
Input"bbbbb"
Expected1
Only one repeated character can stay in the window.
Hidden Test Categories
Empty stringRepeat appears before current left pointerMixed case characters