Skip to content

Latest commit

 

History

History
39 lines (30 loc) · 932 Bytes

File metadata and controls

39 lines (30 loc) · 932 Bytes
 int smallestSubstring(string S) {
        
        int z=0,o=0,s=0;
        int n=S.size();
        int i=0,j=0;
        int ans=INT_MAX;
        
        for(int j=0;j<n;j++)
        {
            if(S[j]=='0')
                z++;
            
            else if(S[j]=='1')
                o++;
                
            else if(S[j]=='2') 
                s++;
                
             while(z!=0 && s!=0 && o!=0)
             {
                 ans=min(ans,j-i+1);
                 if(S[i]=='0')
                    z--;
                 if(S[i]=='1')
                    o--;  
                 if(S[i]=='2')
                    s--;
                
                i++;
             }
       
        }
        
        return (ans == INT_MAX) ? -1 : ans;
    }