# Count of sub-strings that contain character X at least once in C++

C++Server Side ProgrammingProgramming

We are given a string str[] and a character X. The goal is to find the substrings of str[] such that all the substrings contain X at least once. For str[]=”abc '' and X=’a’, the substrings containing ‘a’ at-least once are “a”, “ab”, “abc”. The count is 3.

Let us understand with examples.

Input − str[] = “aabccd” X=’c’

Output − Count of sub-strings that contain character X at least once are − 14

Explanation − Substrings containing at-least one ‘c’ will be : “c”, “c”, “bc”, “cc”, “cd”, “abc”, “bcc”, “ccd”, “aabc”, “abcc”, “bccd”, “aabcc”, “abccd”, “aabccd”.

Input − str[] = “settings” X=’s’

Output − Count of sub-strings that contain character X at least once are − 14

Explanation − Substrings contains at-least one ‘s’ will be : “s”, “s”, “se”, “gs”, “set”, “ngs”, “sett”, “ings”, “setti”, “tings”, “settin”, “ttings”, “setting”, “ettings”, “settings”

## 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 count characters before character X as temp. As soon as X is encountered, strings containing X will have length temp+1. Now we have X number of substrings containing X will be the remaining characters (length-current index) X ( temp+1). Add this to count. Now update temp=0 and proceed for next X till end of string. At the end we have count as the number of sub-strings that contain character X at least once.

• Take a string str and a character x.

• Function sub_x(char str[],int length,char x) takes a string, it’s length, character x and returns the count of substrings that contain character x at least once.

• Take the initial count as 0. Take temp as characters before first x in str[] as 0 initially.

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

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

• If str[i] is not x then increment temp as characters before first x.

• If str[i] == x then length of string including x will be temp+1. Remaining characters of str[] will be length-i.

• All substrings will be ( temp+1) * (length-i). Add this to count. Now update temp=0 for next iteration.

• Do this until the end of str[] is reached.

• Return count as result at the end.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
int sub_x(string str, int length, char x){
int count = 0;
int temp = 0;
for (int i = 0; i < length; i++){
if (str[i] == x){
int temp_2 = temp + 1;
count = count + temp_2 * (length - i);
temp = 0;
}
else{
temp++;
}
}
return count;
}
int main(){
string str = "abcabbc";
int length = str.length();
char x = 'a';
cout<<"Count of sub-strings that contain character X at least once are: "<<sub_x(str, length,
x);
return 0;
}

## Output

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

Count of sub-strings that contain character X at least once are: 19