Understanding Longest Substring
Given a string s, find the length of the longest contiguous substring that contains no repeated characters. A substring must preserve consecutive characters; it is not a subsequence.
How it works
A duplicate can only be repaired by shrinking the current window from the left. Neither pointer ever needs to move backward.
Maintain a set matching the active window. Before inserting the next character, remove characters from the left until the duplicate disappears. Each character enters and leaves at most once.
Try an example
Input: s = "abcabcbb"
Output: 3
"abc" is a longest valid substring.
Input: s = "pwwkew"
Output: 3
"wke" is contiguous; "pwke" is not.
Constraints
- Characters are Unicode code points; whitespace is significant.
- An empty string has a longest substring of length 0.
- Visualization limit: 64 code points.
Use the simulator
Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.
Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.