LeetCode #1358

Number of Substrings Containing All Three Characters

1개의 풀이 · Python

문제 원문 보기 ↗

SOLUTION INFO

Python · main.py

main.py
from collections import defaultdict

class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        N = len(s)
        r = -1
        D = defaultdict(int)
        ans = 0
        for i in range(N):
            while r + 1 < N and len(D) < 3:
                D[s[r + 1]] += 1
                r += 1
            if len(D) == 3:
                ans += N - r
            D[s[i]] -= 1
            if D[s[i]] == 0:
                D.pop(s[i])
        return ans

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.