Not every window has a fixed size. A variable-size sliding window expands while some rule holds and shrinks from the left the moment it breaks. Because each index enters and leaves the window at most once, the total work is O(n).
A keystroke log is stored as a string s, one character per key press. Find the longest unbroken stretch of the log (a substring) in which no character turns up twice, and return its length.
Examples
Input: s = "streets"
Output: 4
Explanation: "stre" is the longest such substring.
Input: s = "loop"
Output: 2
Explanation: "lo" or "op" (note "lop" is a subsequence, not a substring).
Input: s = ""
Output: 0
Constraints
0 <= len(s) <= 10**4smay contain letters, digits, spaces and symbols.- Aim for O(n) time.
Goals
- Grow a variable-size window on the right and shrink it on the left when a rule is broken
- Use a set or dict to know in O(1) whether the window contains a character
- Track the best window length while the window changes size