# Number of Substrings Containing All Three Characters (Leetcode 1358)

Problem Link: <https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/>

```python
class Solution:
    def numberOfSubstrings(self, s: str) -> int:
               
        def atmostK(s,k):
            n=len(s)
            c=0
            j=0
            d={}
            ans=0
            for i in range(n):
                if(s[i] not in d):
                    d[s[i]]=1
                else:
                    d[s[i]]+=1
                    

                while(len(d)>k):
                    if(d[s[j]]==1):
                        del d[s[j]]
                        j+=1
                    else:
                        d[s[j]]-=1
                        j+=1
                        
                ans=ans+(i-j+1)
                
            return ans
        
        return atmostK(s,3)-atmostK(s,2)
            
            
            
            
                
                
        
        
                
                
        
```
