Given a string str consisting only of the characters ‘a’, ‘b’ and ‘c’, find the number of sub-strings that do not contain all the three characters at the same time.
Input: str = “abc”
The possible substrings are “a”, “b”, “c”, “ab” and “bc”
Input: str = “babac”
Approach: The idea is to use three variables a_index, b_index and c_index to store the latest occurrence of the characters a, b and c and another variable ans to store the number of substrings that don’t have at least one of a, b or c and initialize its value to the total number of substrings with length n i.e, n*(n+1)/2, where n is the length of the string.
Now simply traverse the string from the beginning. At each point update the value of variables to the latest value whenever encountered.Since we are indexing with 0, so we are updating the index as i+1 at each step.
Also at each step, you need to find the index of minimum occurrence of the remaining of the two characters not encountered in the current step.
Then simply deduct this index from the variable ans. This is because once you found the index of the minimum of the remaining characters you are sure that any more substrings formed by moving downwards in index will also contain all the three characters.
Hence the total numbers of all such substrings(ones containing all a, b, and c) formed at this step is the found index.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check whether two strings contain same characters in same order
- Count distinct substrings that contain some characters at most k times
- Count substrings that contain all vowels | SET 2
- Count of substrings of a given Binary string with all characters same
- Count the nodes of a tree whose weighted string does not contain any duplicate characters
- Find substrings that contain all vowels
- Count substrings with same first and last characters
- Recursive solution to count substrings with same first and last characters
- Count number of substrings of a string consisting of same characters
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Next word that does not contain a palindrome and has characters from first k
- Count of all unique substrings with non-repeating characters
- Count of substrings having all distinct characters
- Maximize count of non overlapping substrings which contains all occurrences of its characters
- Min flips of continuous characters to make all characters same in a string
- Minimum characters to be replaced to make frequency of all characters same
- Count of substrings formed using a given set of characters only
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count of numbers in a range that does not contain the digit M and which is divisible by M.
- Count of strings that does not contain Arc intersection
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.