Skip to content

Sliding Window Variable Subarrays Longer

Table of Contents

1358. Number of Substrings Containing All Three Characters

1358. Number of Substrings Containing All Three Characters - Python Solution
from collections import defaultdict


# Sliding Window Variable Size
def numberOfSubstrings(s: str) -> int:
    freqs = defaultdict(int)
    res = 0
    left = 0

    for right in range(len(s)):
        freqs[s[right]] += 1

        while len(freqs) == 3:
            freqs[s[left]] -= 1
            if freqs[s[left]] == 0:
                del freqs[s[left]]
            left += 1

        res += left

    return res


s = "abcabc"
print(numberOfSubstrings(s))  # 10

2962. Count Subarrays Where Max Element Appears at Least K Times

3325. Count Substrings With K-Frequency Characters I

2799. Count Complete Subarrays in an Array

2537. Count the Number of Good Subarrays

3298. Count Substrings That Can Be Rearranged to Contain a String II

2495. Number of Subarrays Having Even Product

Comments