Longest Substring Without Repeating Characters

Asked byAmazonMicrosoftFacebookBloombergAppleGoogle

Problem

Given a string s, find the length of the longest substring that does not contain any repeating characters.

Examples

Example 1
Input:s = "abcabcbb"
Output:3
The answer is "abc".
Example 2
Input:s = "bbbbb"
Output:1

Constraints

  • 0 <= s.length <= 5 * 10^4

Solve it in the editor. Sign in free to run your Python or JavaScript against test cases, get a verdict, and track your attempts.

Solve on FeatCode →

How to approach it: the Sliding Window pattern

A window — a contiguous subarray or substring — expands and shrinks as it moves across the input, so you track a running condition instead of recomputing it from scratch for every possible window.

Look for this pattern when

  • The problem asks for the longest/shortest/best contiguous subarray or substring meeting a condition.
  • Brute force would recompute a sum or count for every possible window — a sign that work can be shared between overlapping ranges.

Read the full Sliding Window guide →

Video walkthroughs

Original problem on LeetCode ↗

More Sliding Window problems