LeetCode 3 Longest Substring Without Repeating Characters - Why Naive Fails

The trap

Check all substrings. For each substring, scan to verify all characters are unique.

There are O(n2)O(n^2) substrings. Checking each for uniqueness takes O(n)O(n). Total: O(n3)O(n^3).

For n=5×104n = 5 \times 10^4, that's about 101410^{14} operations. Far too slow.