30:00
Given a string s, return the maximum length of a substring in which every character appears at most two times.
s
2≤s.length≤1002 \leq \text{s.length} \leq 1002≤s.length≤100
s consists only of lowercase English letters.
Run code to see results.
Every letter gets two pegs. Grow the window to the right; if a letter would need a third peg, shrink from the left until it fits again.
s with the window
b
L
c
a
peg board: two pegs per letter
window = empty
best = 0
Slide a window across the string and keep a count for each letter inside it. When a letter shows up a third time, move the left edge forward until it is back to two, then record the window length.