Sliding Window Counting windows
184. Number of Substrings Containing All Three Characters
You are given a string s made of the letters a, b and c. Return the number of substrings of s that
hold each of the three letters at least once.
A substring is a block of letters that sit next to each other in s, with at least one letter. Two
substrings are different if they start or end at different positions, even when they hold the same letters.
Example 1
Input: s = "abcba"
5Explanation: Five substrings hold all three letters: abc, abcb, abcba, bcba and cba. The others, such as
bcb, miss a letter.
Example 2
Input: s = "aaacb"
3Explanation: A substring with all three letters must reach the b at the end and start at one of the three as. That
gives aaacb, aacb and acb.
Example 3
Input: s = "ccbcbb"
0Explanation: The string has no a, so no substring holds all three letters.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ s.length ≤ 105sholds only the lettersa,bandc.
Clarifying questions
What you would ask an interviewer before writing any code.