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
DNA Sequence AnalysisDNA Sequence:ABCABCPosition Tracking SystemLast A: 3Position 3Index in sequenceLast B: 4Position 4Index in sequenceLast C: 5Position 5Index in sequenceValid Segments Calculationmin(3, 4, 5) + 1 = 4Segments ending at current positionAdd to total count and continueComplete set found!
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only storing three position variables regardless of input size

n
2n
โœ“ 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
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18 Apple 15
48.2K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen