1358. Number of Substrings Containing All Three Characters

Link

Solution

 public int numberOfSubstrings(String s) {
        int start = 0, ret = 0;
        int[] count = new int[3];
        for(int end = 0; end < s.length(); end++){
            count[s.charAt(end) - 'a']++;
            while(count[0] > 0 && count[1] > 0 && count[2] > 0){
                count[s.charAt(start) - 'a']--;
                start++;
            }
            ret += start; //the start just stands for the possible count in the problem
        }
        return ret;
    }

Last updated

Was this helpful?