⌘K
Flycodx

Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters

MediumStrings44% accepted

Given a string s, find the length of the longest substring that contains no repeating characters. A substring is a contiguous sequence of characters within the string. Example: s="abcabcbb" → 3 (substring "abc") Example: s="bbbbb" → 1 (substring "b") Example: s="pwwkew" → 3 (substring "wke")

Input format — only if your code reads from stdin

Input format: - A single string s Output: a single integer — the length of the longest substring without repeating characters.

Approach

Sliding window with a hash map: maintain a window [left, right]. Advance right pointer; if s[right] is already in the window, move left pointer past the previous occurrence. Track the maximum window size at each step.

Input

abcabcbb

Output

3
  • •0 ≤ len(s) ≤ 5×10^4, s consists of English letters, digits, symbols, and spaces
solution.py
Loading editor...
Test 1
Test 2
Test 3

Press Run to test your custom input, or Submit to judge against all test cases.

Sign in to continue

Create a free account to run code, submit solutions and track progress.

  • Free forever — no credit card
  • 10+ languages with live execution
  • Points, streaks & leaderboards
Auto Save OnLn 1, Col 1Spaces: 4UTF-8LFPython 3