# Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time in C++

C++Server Side ProgrammingProgramming

We are given a string str[] containing ‘a’, ‘b’ and ‘c’ only. The goal is to find the substrings of str[] such that all the three characters are not part of that substring. For any string str, substrings could be “a”, “b”, “c”, “abb”, “bba”, “bc”, “ca”, “ccc” but not “abc”, “bcca” , “cab” as these have ‘a’, ‘b’ and ‘c’, all three.

Let us understand with examples.

Input − str[] = “aabc”

Output − Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time are − 8

Explanation − Substrings will be : “a”, “a”, “b”, “c”, “aa”, “ab”, “bc”, “aab”

Input − str[] = “abcabc”

Output − Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time are − 11

Explanation − Substrings will be : “a”, “b”, “c”, “a”, “b”, “c”, “ab”, “bc”, “ca”, “ab”, “bc”

## Approach used in the below program is as follows

In this approach we know that total number of substrings of string with n characters is n*(n+1)/2.

We will now traverse the string and for each type of character ‘a’, ‘b’ or ‘c’. We will check for previous indexes of the other two characters (‘b’,’c’), (‘c’,’a’) and (‘a’, ‘b’). Just subtract the minimum index of the other two from count as we know that we are removing that character to include current character in substring so that it does not contain all three.

• Take a string str as a character array.

• Function sub_without_all(char str[], int size) takes a string, it’s length and returns the count of substrings that do not contain ‘a’, ‘b’ and ‘c’ all together.

• Take the initial count size*(size+1)/2 for number of all possible substrings of str[].

• Take variables a,b,c to store the last index of ‘a’, ‘b’, ‘c’ in str[]. Initialize all with 0.

• Traverse str[] using for loop from i=0 to i<size.

• If str[i]==’a’ update index of ‘a’ as a=i+1. Subtract minimum of index of ‘b’ or ‘c’ from count to include ‘a’ in substring. Subtract b,c whichever is minimum from count.

• Do similar as the previous step for str[i]==’b’ or str[i]==’c’.

• At the end we have count as substrings of str[] without all three characters in them at once.

• Return count as result.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
int sub_without_all(char str[], int size){
int update_size = size * (size + 1);
int count = update_size / 2;
int a, b, c;
a = b = c = 0;
for (int i = 0; i < size; i++){
if (str[i] == 'a'){
a = i + 1;
count -= min(b, c);
}
else if (str[i] == 'b'){
b = i + 1;
count -= min(a, c);
}
else{
c = i + 1;
count -= min(a, b);
}
}
return count;
}
int main(){
char str[] = "abcabbc";
int size = strlen(str);
cout<<"Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at
the same time are: "<<sub_without_all(str, size);
return 0;
}

## Output

If we run the above code it will generate the following output −

Count of sub-strings that do not contain all the characters from the set {‘a’, ‘b’, ‘c’} at the same time are: 15
Published on 02-Dec-2020 16:42:46