# Count substrings made up of a single distinct character

In this article, we'll discuss the problem of counting the number of substrings in a given string that consist of a single distinct character. We'll explore an efficient algorithm for solving this problem and provide C++ code to implement it.

## Problem Statement

Given a string S, the task is to count the number of substrings that are made up of a single distinct character.

For example, if the input string is "aaaaa", then the output should be 15, because there are 15 substrings that consist of a single distinct character. The substrings are "a", "a", "a", "a", "a", "aa", "aa", "aa", "aa", "aaa", "aaa", "aaa", "aaaa", "aaaa", "aaaaa".

## Algorithm

We can solve this problem in linear time complexity. We can iterate through the input string and keep track of the current character and the length of the current substring. Whenever we encounter a new character or reach the end of the string, we can count the number of substrings that can be formed using the current character and the length of the current substring.

Here's the step-by-step algorithm to solve this problem −

• Initialize count and len to 1.

• Iterate through the string S from index 1 to n-1.

• If the current character is the same as the previous character, increment len by 1.

• If the current character is different from the previous character, add (len*(len+1))/2 to count, reset len to 1.

• Return count.

Let's take the example of the string "aaaaa" to understand the algorithm −

• Initialize count and len to 1.

• Iterate through the string from index 1 to n-1:

• At index 1, the current character is the same as the previous character, so increment len by 1.

• At index 2, the current character is the same as the previous character, so increment len by 1.

• At index 3, the current character is the same as the previous character, so increment len by 1.

• At index 4, the current character is the same as the previous character, so increment len by 1.

• We have reached the end of the string, so add (len*(len+1))/2 to count. count = count + (5*(5+1))/2 = 15.

• Return count.

## C++ Implementation

Here's the C++ code to implement the above algorithm −

### Example

#include<bits/stdc++.h>
using namespace std;

int countSubstrings(string S) {
int n = S.length();
int count = 1, len = 1;
for (int i = 1; i < n; i++) {
if (S[i] == S[i-1]) {
len++;
} else {
count += (len*(len+1))/2;
len = 1;
}
}
count += (len*(len+1))/2;
return count-1;
}

int main() {
string S = "aaaaa";
int count = countSubstrings(S);
cout << count << endl;
return 0;
}


### Output

15


## Conclusion

In this article, we've discussed the problem of counting the number of substrings in a given string that consist of a single distinct character. We've provided an efficient algorithm to solve this problem in linear time complexity and implemented it in C++. This problem can be solved using other techniques as well, but the above algorithm provides

Updated on: 17-May-2023

46 Views 