← Visualizers
Sliding Window · variable window, sub-variant F — count LeetCode 1358

Number of Substrings Containing All Three Characters / visualized

Fix the right end at r and ask: how many starts make a valid substring? A substring [s..r] contains an a, a b and a c exactly when it reaches back past the most recent occurrence of all three — so keep last[a], last[b], last[c] and take m = min(last). Every start s ≤ m works and every start s > m misses whichever character sits at m, so the step banks exactly m + 1. That minimum is not a helper value — it is the left boundary of the answer, which is why the page draws it as the pivot. The shrinking-window formulation works too: hold l, advance it while [l..r] still contains all three, then add l — and it lands on the same number, because that l ends up equal to m + 1. It is just more fragile: it needs a frequency map with matching increments and decrements, an inner loop, and a decision about whether to add before or after shrinking. The last-occurrence form has none of those, and the −1 sentinel makes the "not all three seen yet" case fall out of the arithmetic on its own — m = −1 adds 0, so there is no special case to forget.

Execution

idle
s  ·  green = a legal start  ·  amber = the pivot m
Press Run to begin.
0 / 0
Speed

Java · running line

legal start (counted) m = min(last) — the pivot minimal window [m..r] r, the fixed right end not reached yet
O(n) time · O(1) space  ·  three integers, one pass, no inner loop and no frequency map. Sub-variant F: the answer is a running count, and every substring is counted exactly once — at the step where its right end is reached. The whole problem reduces to count += min(last[a], last[b], last[c]) + 1.