← Visualizers
Sliding Window · anti-pattern LeetCode 395

Longest Substring with At Least K Repeating Characters / visualized

Every sliding window rests on one property: validity must be monotone — once a window goes bad, growing it must keep it bad, so shrinking is the only sane move. “Every character appears at least k times” is the opposite: an invalid window can be repaired by adding more characters. a is invalid at k=2, and ababb is valid. So there is no shrink rule, and Act 1 shows the window eating itself alive. Act 2 restores monotonicity by fixing a parameter: pin the number of distinct characters to d, and “too many distinct” is monotone, so a normal window works — run it once for each d from 1 to 26 and take the best. The other standard fix is divide and conquer: any character whose total count in a segment is below k can never appear in the answer, so split the segment on it and recurse. Same answer, different lever — but only the fixed-d version teaches you what to do the next time a window refuses to shrink.

Execution

idle
s  the string
distinct budget  d  — fixed for one whole pass
counts inside the window
Press Run to begin.
0 / 0
Speed

Java · running line

act 1
left right valid window · recorded the character forcing the shrink evicted · the answer needed it
Act 1 O(26n) and wrong · Act 2 O(26 · 26n) = O(n) for a fixed alphabet, O(26) space  ·  the transferable move: when a predicate is not monotone, look for a hidden parameter, fix it, and the window comes back — you pay one pass per value of the parameter.