Given a string s
consisting only of characters a, b and c.
Return the number of substrings containing at least one occurrence of all these characters a, b and 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: 1
Constraints:
3 <= s.length <= 5 x 10^4
s
only consists of a, b or c characters.
思路:two pointer
class Solution(object):
def numberOfSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
n=len(s)
i=j=0
d={'a':0,'b':0,'c':0}
res=0
while j<n:
d[s[j]] += 1
j += 1
while min(d.values())>0:
res+=n-j+1
d[s[i]] -= 1
i+=1
return res
s=Solution()
print(s.numberOfSubstrings("abcabc"))