思路:
def numberOfSubstrings(self, s):""":type s: str:rtype: int"""def findmax(s):win = collections.Counter()l,count = 0,0res = 0for r in range(len(s)):win[s[r]] += 1if win[s[r]] == 1:count += 1while count == 3:win[s[l]] -= 1if win[s[l]] == 0:count -= 1l += 1res += r-l+1return resres = (len(s)**2 + len(s))/2return res - findmax(s)
解法二
找到一个满足条件的之后,在他之后的所有字符都满足条件
所以加上它本身就是len(s)-r
之后移动左指针,直到它不符合参考:
https://leetcode.cn/problems/number-of-substrings-containing-all-three-characters/solution/si-kao-de-guo-cheng-bi-da-an-zhong-yao-xiang-xi-tu/
def numberOfSubstrings(self, s):""":type s: str:rtype: int"""win = collections.Counter()l,count = 0,0res = 0for r in range(len(s)):win[s[r]] += 1if win[s[r]] == 1:count += 1while count == 3:res += len(s)-rwin[s[l]] -= 1if win[s[l]] == 0:count -= 1l += 1return res