Example 1
- Input
- s = "abcabcbb"
- Output
- 3
The longest substring without repeating characters is "abc", with length 3.
Given a string s, return the length of the longest contiguous substring in which every character appears exactly once.
Given a string s, return the length of its longest contiguous substring with no repeated character. Characters next to each other in the answer must occupy adjacent positions in s; skipping a repeated character would create a subsequence, not a substring.
Return 0 for an empty input. Letter case, digits, punctuation, and spaces all participate in the same uniqueness rule.
0 <= s.length <= 10^5s consists of English letters, digits, symbols and spaces.The longest substring without repeating characters is "abc", with length 3.
Every longer window repeats "b", so the answer is "b" with length 1.
The longest substring without repeating characters is "wke", with length 3.
There is no non-empty substring, so the maximum length is zero.
The repeated b forces removal of both a and the earlier b. The longest valid windows are ab and ba, each length two.
Lowercase a, a space, uppercase A, digit 1, and ! are five distinct characters. Spaces and case are significant.
Keep a window of consecutive characters and remember exactly which characters it contains. In abcabcbb, the first abc is unique, but the next a repeats the window's a. Dropping that earlier a leaves bca, which is still a length-three candidate. Remove only enough of the left side to eliminate a repeat; preserving the remaining characters lets later windows grow without restarting.
Reveal Brute Force: intuition, complexity, and code
A sliding window plus a set turns uniqueness constraints into a one-pass scan: expand until the rule breaks, then shrink from the left just enough to restore it.