Back to Practice
#0234

Longest Substring Without Repeating Characters

MediumDSA28 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 the cleanest sliding-window example for tracking a valid window and moving the left boundary only when needed.

Function Signature

def longest_unique_substring(s):

Examples

Example 1
Inputs = "abcabcbb"
Output3

The longest valid substrings include "abc", length 3.

Constraints

  • Return the exact requested value.
  • Handle empty or small inputs when the prompt allows them.
  • Prefer the target DSA pattern over brute force when the input can grow.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Repeated cycle
Input"abcabcbb"
Expected3

The best substring is length 3.

Left jump case
Input"abba"
Expected2

When the second b appears, left jumps past the first b.

Hidden Test Categories
empty stringall same characterall unique characters