Number of Substrings Containing All Three Characters - Problem
Imagine you're analyzing DNA sequences! You have a string s composed exclusively of three nucleotide bases: a, b, and c. Your task is to find how many contiguous subsequences (substrings) contain at least one occurrence of each of these three characters.
For example, if your DNA sequence is "abcabc", you need to count all substrings that have at least one 'a', one 'b', and one 'c'. The substring "abc" qualifies, as does "abca", "abcab", and the entire string "abcabc".
Goal: Return the total count of such valid substrings.
Input & Output
example_1.py โ Basic Case
$
Input:
s = "abcabc"
โบ
Output:
10
๐ก Note:
The substrings containing at least one occurrence of 'a', 'b', and 'c' are: "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc", "abc" (the second occurrence). Total count = 10.
example_2.py โ Minimum Valid Case
$
Input:
s = "aaacb"
โบ
Output:
3
๐ก Note:
The valid substrings are: "aaacb", "aacb", "acb". These are the only substrings that contain at least one of each character 'a', 'b', and 'c'.
example_3.py โ No Valid Substrings
$
Input:
s = "abc"
โบ
Output:
1
๐ก Note:
Only one substring "abc" contains all three characters. This is the minimum possible valid case.
Visualization
Tap to expand
Understanding the Visualization
1
Track Base Positions
As you scan the DNA sequence, remember where you last encountered each base
2
Calculate Valid Segments
At any position, segments ending here are valid if they start from position 0 up to the minimum last-seen position
3
Count Efficiently
Add min(last_positions) + 1 to your total count for each position
4
Single Pass Complete
One scan through the sequence gives you the complete answer
Key Takeaway
๐ฏ Key Insight: Instead of checking every possible substring, track the last positions of each required character. The number of valid substrings ending at any position is simply the minimum of these positions plus one!
Time & Space Complexity
Time Complexity
O(n)
Single pass through the string, constant time operations at each step
โ Linear Growth
Space Complexity
O(1)
Only storing three position variables regardless of input size
โ Linear Space
Constraints
- 3 โค s.length โค 5 ร 104
- s consists only of 'a', 'b', and 'c' characters
- The string contains at least one character of each type for non-zero results
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code