↓Skip to main content
  1. LeetCode/

LeetCode 1358: Number of Substrings Containing All Three Characters

·2 mins· ·
LeetCode Medium Sliding-Window Hash-Table String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-19

  • Submissions: Wrong Answer → Accepted

Approach
#

The same “at least” counting pattern as LeetCode 2962: while all three characters are present, keep shrinking the left until one is missing; every left end in [0, left) is valid, so count += left.

My Attempts
#

Attempt 1 used len(set(s)) as the target number of kinds instead of a fixed 3. If the string has only two kinds, e.g. s = "abb", the target became 2 and every substring with a and b counted, returning 2; without a c nothing is valid, so the answer is 0.

Solution
#

class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        cnt = Counter()
        left = count = 0
        for c in s:
            cnt[c] += 1
            while len(cnt) == 3:                   # all three present: keep shrinking
                cnt[s[left]] -= 1
                if cnt[s[left]] == 0:
                    del cnt[s[left]]
                left += 1
            count += left
        return count

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • The target is always 3 kinds, not however many appear in the string.

Interview Explanation
#

This is an “at least” count: while all three characters are in the window I keep shrinking the left until one is missing, and then every left end from 0 to left - 1 gives a substring with all three, so I add left. Time O(n).

Related

LeetCode 3325: Count Substrings With K-Frequency Characters I
·2 mins
LeetCode Medium Sliding-Window Hash-Table String
LeetCode 3325 note: an “at least one character appears k times” counting window; when the new character reaches k, keep shrinking until it drops to k - 1
LeetCode 395: Longest Substring with At Least K Repeating Characters
·3 mins
LeetCode Medium Sliding-Window Divide-and-Conquer Hash-Table String
LeetCode 395 note: “every character at least k times” is not monotonic, so a direct slide fails; divide and conquer splits on characters with fewer than k occurrences, or fixing the number of distinct characters makes the window slidable
LeetCode 2516: Take K of Each Character From Left and Right
·2 mins
LeetCode Medium Sliding-Window Hash-Table String
LeetCode 2516 note: taking from both ends leaves a middle block in which each letter may appear at most total − k times; find the longest such window
LeetCode 1016: Binary String With Substrings Representing 1 To N
·3 mins
LeetCode Medium Sliding-Window String Bit-Manipulation Hash-Table
LeetCode 1016 note: s has at most 1000 characters, so there are few substrings of any fixed length and a large n must be False; checking from n downward fails fast, or extend up to 30 bits from each start to collect every value <= n
LeetCode 3694: Distinct Points Reachable After Substring Removal
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table String
LeetCode 3694 note: the endpoint is the total displacement minus the removed segment’s displacement; a length-k window tracks that segment and each endpoint goes into a set
LeetCode 187: Repeated DNA Sequences
·1 min
LeetCode Medium Hash-Table String Bit-Manipulation Sliding-Window
LeetCode 187 note: count every length-10 substring in a hash map and return those seen more than once