Lassi_BKL

 avatar
bruteCoder
java
a year ago
818 B
5
Indexable
class Solution {
    public int smallestSubstring(String S) {
        // Code here
        int z = 0 ;
        int o = 0;
        int t = 0;
        
        char[] arr = S.toCharArray();
        int ans = arr.length+1;
        
        int left = 0 ;
        int right = 0 ;
        
        for(;right < arr.length;right++)
        {
            if(arr[right] == '0') z +=1;
            else if(arr[right] == '1') o +=1;
            else t += 1;
            
            while(z>0 && o >0 && t>0)
            {
                ans = Math.min(ans,right-left+1);
                
                if(arr[left] == '0') z-=1;
                else if(arr[left] == '1') o -= 1;
                else t -= 1;
                left +=1;
            }
        }
        
        return ans == arr.length+1 ? -1 : ans ;
    }
};
Editor is loading...
Leave a Comment