Understanding the Problem
We are given a string s
that only contains the characters 'a'
, 'b'
, and 'c'
. Our task is to count the number of non-empty substrings of s
that contain at least one occurrence of each of the three characters.
In simpler terms, we want to find all possible substrings that have all three characters — 'a'
, 'b'
, and 'c'
— at least once, no matter their order.
Step-by-Step Solution with Example
Step 1: Take the example s = "abcabc"
We will use a two-pointer sliding window approach to move through the string and maintain a window that always tries to include all three characters. The goal is to count how many substrings end at each position that contain all three characters.
Step 2: Initialize pointers and tracking structures
- Start with two pointers:
left = 0
and right
that moves from 0 to end of string.
- Use a dictionary
count
to keep track of how many 'a', 'b', and 'c' are in the current window.
- Initialize
total = 0
to store the count of valid substrings.
Step 3: Move the right pointer and update the count
As right
increases, we add s[right]
to our count map. This helps us know how many of each character are in the current window.
Step 4: Check if window is valid
Each time the window contains at least one of 'a'
, 'b'
, and 'c'
, we know it is a valid window. From here, we can say all substrings that start at left
and end anywhere from right
to the end of the string are valid.
So, we add (length - right)
to total
.
Step 5: Shrink the window from the left
We then reduce the window from the left side by removing s[left]
from the count and moving left
forward. This helps us find the next possible smaller window that still contains all three characters.
Step 6: Repeat until right reaches the end
Continue the above process until right
has scanned the entire string. At each step, we keep updating our total
count.
Final Result for s = "abcabc"
The total valid substrings that contain all three characters are 10. Some examples include: "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc", "abc".
Edge Cases
- Short Strings: If the string has less than 3 characters, it’s impossible to contain all three characters. Return 0.
- Missing Character: If any one of 'a', 'b', or 'c' is not present in the string at all, return 0.
- Repeated Characters: If string is something like "aaabbbccc", it may take longer for a valid window to form, but the logic still holds.
- All characters same: For example, "aaaa" → No substring will contain all three characters. Return 0.
Final Thoughts
This problem is a great example of how sliding window and two pointer techniques help avoid brute force approaches. Instead of checking all substrings explicitly (which would take O(n²) time), we only scan the string once and use a smart window adjustment strategy to compute the answer in linear time.
As a beginner, focus on understanding how the left
and right
pointers move and how the count map helps us decide whether a window is valid.
Comments
Loading comments...