🎢 ELI5 Park / Sliding Window / Longest Substring Without Repeating
Medium

Longest Substring Without Repeating

👶 The 5-Year-Old Summary

Find the longest unique character substring! "abcabcbb" → "abc" = 3! "bbbbb" → "b" = 1!

🎮 Interactive Visualizer

s = "abcabcbb"

🧠 The Brain Hack

Hash set + sliding window! Add char, if duplicate found, shrink from left until no duplicate. Track max length!

💻 The Clean Solution

def length_of_longest_substring(s):
    char_set = set()
    left = 0
    max_length = 0
    
    for right in range(len(s)):
        # Shrink window while duplicate
        while s[right] in char_set:
            char_set.remove(s[left])
            left += 1
        
        # Add current char
        char_set.add(s[right])
        max_length = max(max_length, right - left + 1)
    
    return max_length

# "abcabcbb" → 3 ("abc")
# "bbbbb" → 1 ("b")

Time: O(n)

Space: O(min(n, alphabet))