Substrings of Size Three with Distinct Characters (Leetcode 1876)

Problem Link: https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/

class Solution:
    def countGoodSubstrings(self, s: str) -> int:
        
        n=len(s)
        d={}
        i=0
        j=0
        ans=0
        while(j<n):
            if(s[j] not in d):
                d[s[j]]=1
            else:
                d[s[j]]+=1
            
            if(j-i+1<3):
                j+=1
            
            elif(j-i+1==3):
                if(len(d)==3):
                    ans+=1
                if(d[s[i]]==1):
                    del d[s[i]]
                else:
                    d[s[i]]-=1
                
                i+=1
                j+=1
        
        return ans

Last updated