<jupyter_start><jupyter_text>说明：    给定一个仅由字符a，b和c组成的字符串s。    返回至少包含所有这些字符a，b和c的子字符串的数目。Example 1:    Input: s = "abcabc"    Output: 10    Explanation:         The substrings containing at least one occurrence of the characters a, b and c are         "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" and "abc" (again).         Example 2:    Input: s = "aaacb"    Output: 3    Explanation: The substrings containing at least one occurrence of the characters a, b and c are "aaacb", "aacb" and "acb". Example 3:    Input: s = "abc"    Output: 1Constraints:    1、3 <= s.length <= 5 x 10^4    2、s only consists of a, b or c characters.<jupyter_code>class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        letters = {'a', 'b', 'c'}
        N = len(s)
        count = 0
        
        for gap in range(3, N + 1):
            for start in range(N - gap + 1):
                sub_str = s[start:start + gap]
                if set(sub_str) == letters:
                    count += 1
        return count
from collections import Counter

class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        def count_letter(ct):
            if ct['a'] and ct['b'] and ct['c']:
                return True
            return False
        
        count = 0
        for i in range(len(s)):
            ctr = Counter(s[:len(s) - i])
            for j in range(i + 1):
                if j != 0:
                    start_idx = j - 1
                    end_idx = len(s) + start_idx - i
                    ctr[s[start_idx]] -= 1
                    ctr[s[end_idx]] += 1
                if count_letter(ctr):
                    count += 1
        return count
class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        a, b, c = 0, 0, 0
        ans, i, n = 0, 0, len(s)
        for j, letter in enumerate(s):
            if letter == 'a':
                a += 1
            elif letter == 'b':
                b += 1
            else:
                c += 1
            while a and b and c:
                ans += n - j
                print(n - j, n, j)
                if s[i] == 'a':
                    a -= 1
                elif s[i] == 'b':
                    b -= 1
                else:
                    c -= 1
                i += 1
        return ans
solution = Solution()
solution.numberOfSubstrings('abcbb')
a = {'a', 'c', 'b'}
b = {'a', 'b', 'c'}
print(a == b)
class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        a = b = c = 0                        # counter for letter a/b/c
        ans, i, n = 0, 0, len(s)             # i: slow pointer
        for j, letter in enumerate(s):       # j: fast pointer
            if letter == 'a': a += 1         # increment a/b/c accordingly
            elif letter == 'b': b += 1
            else: c += 1
            while a > 0 and b > 0 and c > 0: # if all of a/b/c are contained, move slow pointer
                ans += n-j                   # count possible substr, if a substr ends at j, then there are n-j substrs to the right that are containing all a/b/c
                if s[i] == 'a': a -= 1       # decrement counter accordingly
                elif s[i] == 'b': b -= 1
                else: c -= 1
                i += 1                       # move slow pointer
        return ans<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11951000896325067, 'fraction_numerical': 0.015237526142814461, 'mean_word_length': 1.3121546961325967, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '3443', 'license': 'Apache-2.0', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1032, 'n_words': 371, 'path': 'String/1012/1358. Number of Substrings Containing All Three Characters.ipynb', 'repo_name': 'YuHe0108/Leetcode', 'size': '3347'}