Number of Substrings Containing All Three Characters
Number of Substrings Containing All Three Characters is LeetCode 1358 (Medium). The string s uses only the letters a, b and c.
- Return how many substrings contain at least one
a, at least oneband at least onec. - With up to 50,000 characters there are over a billion substrings, so they have to be counted without listing them.
- 3 <= s.length <= 5 * 10⁴
- s only consists of 'a', 'b' or 'c' characters.
Intuition
Fix where the substring ends, at index i. Moving its start further left only adds letters, so once a start works, every start to its left works too. The good starts are 0 up to some latest start.
That latest start is the smallest of the three last-seen positions of a, b and c up to i. Start one step later and you skip the only remaining copy of that letter. So exactly min(last) + 1 substrings end at i.
Keep the three positions as you scan and add min(last) + 1 at every index. A letter not seen yet has position −1, which adds 0, as it should.
"Count substrings that contain at least one of each" is monotone: once a substring qualifies, stretching it keeps it qualified. Counting by the end index, with the latest start that still qualifies, turns it into one pass.
Approach
Before reading on, fix the end of a substring at index i and ask which start points give a substring with all three letters. Is there a pattern in which starts work? Then think about what you need to remember to find the last start that works.
Start with no letter seen
Set last = [-1, -1, -1] for a, b and c, and total = 0. −1 means the letter has not appeared yet, and it makes the count below come out to 0 for free.
Record the current letter first
At index i, set the current letter's entry in last to i. The substring ends at i, so this letter is inside it, and its latest copy is here.
Add the substrings that end here
Add min(last) + 1 to total. Starts 0 through min(last) each keep at least one copy of every letter, which is min(last) + 1 substrings; any later start loses the letter with the oldest last copy.
Return the total
Each substring is counted once, at the index where it ends, so after the scan total is the answer. One pass with three integers is all it takes.
Code in Python, C++ and Java
Common pitfalls
Starting the last-seen positions at 0
last = [0, 0, 0]
last = [-1, -1, -1]
0 claims every letter was already seen at index 0. For s = "aaa" no substring has all three letters, but each index adds 0 + 1, so the code returns 3 instead of 0.
Dropping the + 1
total += min(last)
total += min(last) + 1
Positions start at 0, so starts 0 through min(last) are min(last) + 1 choices. For s = "abc" the only valid substring is the whole string, but min(last) is 0 there and the code returns 0.
Counting before recording the current letter
total += min(last) + 1
last[ord(ch) - ord("a")] = ilast[ord(ch) - ord("a")] = i
total += min(last) + 1The current letter is part of every substring that ends here. Counting first ignores it: for s = "abc" the c at index 2 is still marked unseen when the count is taken, so the answer comes out 0 instead of 1.
Edge cases
"aabb"That letter's position stays −1 for the whole scan, so every index adds 0 and the answer is 0. The tracer's third example shows it.
Complexity
Number of Substrings Containing All Three Characters FAQ
Can a sliding window solve it too?
Yes. Grow the right end and keep letter counts; while the window holds all three letters, shrink it from the left. After shrinking, every start before left was valid for this end, so add left to the total. It is also O(n), but needs an inner loop and a count array.
What if every one of k letters had to appear?
Keep the last position of each of the k letters and add the smallest one plus 1 at each index. The idea is unchanged; only finding the minimum costs O(k) per character, unless the positions are kept in an ordered structure.