Data Structures & Algorithms Sliding Window
💡
Exercise 43

Longest Without Repeating 25 XP Medium

LeetCode Ctrl+Enter Run Ctrl+S Save

Given a string s, find the length of the longest substring without repeating characters.

s = 'abcabcbb' # Output: 3 # 'abc' is the longest without repeats

This is a variable-size sliding window problem. Expand the right boundary. When a duplicate is found, shrink the left boundary until the duplicate is removed.

Use a set to track characters in the current window. Time: O(n), Space: O(min(n, alphabet)).

📋 Instructions
Implement `length_of_longest_substring(s)` using a sliding window with a set. Test with: - `'abcabcbb'` → 3 - `'bbbbb'` → 1 - `'pwwkew'` → 3 Print all three results.
Use a left pointer and a set. For each right char: while char is in set, remove s[left] from set and move left forward. Add s[right] to set. Track max window size.
🧪 Test Cases
Input
length_of_longest_substring('abcabcbb')
Expected
3
Test case 1
Input
length_of_longest_substring('bbbbb')
Expected
1
Test case 2
Input
length_of_longest_substring('pwwkew')
Expected
3
Test case 3
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.