The Number of Substrings Containing All Three Characters problem asks you to count substrings that contain at least one 'a', one 'b', and one 'c'. Instead of checking each substring, the efficient approach uses the "last occurrence" technique or a variable sliding window that counts from the right. This coding problem is a clean variable-window string problem.
Microsoft, Meta, Amazon, TikTok, Google, and Bloomberg ask this because it tests the ability to count "at least one of each" substrings efficiently. The standard approach of extending until all three are covered, then counting how many right-extensions remain valid, is the key insight. The hash table, sliding window, and string interview pattern is the core.
Last occurrence tracking. For each right endpoint r, track the last seen position of 'a', 'b', 'c'. The minimum of these three positions (call it minPos) means every substring starting from index 0 to minPos and ending at r contains all three characters. Add (minPos + 1) to the count for each r.
s = "abcabc". Track last_a, last_b, last_c (initialized to -1).
minPos + 1 as the number of valid start positions — this is correct but often confused."Count substrings containing all k characters" problems use the last-occurrence trick: once you've seen all required characters, the earliest last-occurrence position determines how many starting positions give valid substrings. Adding (minPos + 1) per right endpoint is the elegant O(n) solution. Practice extending this to "all k distinct characters" and "at least one of each of m characters" — the same technique applies.