- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Here are the programs that implements the above algorithm −
#include <stdio.h> #include <string.h> int countSubstrings(char S[]) { int n = strlen(S); int count = 1, len = 1; // Initialize counters for substrings and consecutive characters for (int i = 1; i < n; i++) { if (S[i] == S[i - 1]) { // Check if the current character is the same as the previous one len++; // If yes, increment the length of consecutive characters } else { count += (len * (len + 1)) / 2; // Calculate and accumulate substrings formed by consecutive characters len = 1; // Reset the length for new set of consecutive characters } } count += (len * (len + 1)) / 2; // Account for the last set of consecutive characters return count - 1; // Return the total count of substrings } int main() { char S[] = "aaaaa"; int count = countSubstrings(S); printf("%d\n", count); return 0; }
Output
15
#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
public class SubstringCounter { public static int countSubstrings(String S) { int n = S.length(); // length of the input string int count = 1, len = 1; // Initialize counters for substrings and consecutive characters for (int i = 1; i < n; i++) { if (S.charAt(i) == S.charAt(i - 1)) { // Check if the current character is the same as the previous one len++; // If yes, increment the length of consecutive characters } else { count += (len * (len + 1)) / 2; // Calculate and accumulate substrings formed by consecutive characters len = 1; // Reset the length for new set of consecutive characters } } count += (len * (len + 1)) / 2; // Account for the last set of consecutive characters return count - 1; // Return the total count of substrings } public static void main(String[] args) { String S = "aaaaa"; int count = countSubstrings(S); System.out.println(count); } }
Output
15
def count_substrings(S): n = len(S) count = 1 # Initialize counters for substrings length = 1 # Initialize counter for consecutive characters for i in range(1, n): if S[i] == S[i - 1]: # Check if the current character is the same as the previous one length += 1 # If yes, increment the length of consecutive characters else: count += (length * (length + 1)) // 2 # Calculate and accumulate substrings formed by consecutive characters length = 1 # Reset the length for new set of consecutive characters count += (length * (length + 1)) // 2 # Account for the last set of consecutive characters return count - 1 def main(): S = "aaaaa" count = count_substrings(S) # Call the function to count substrings print(count) if __name__ == "__main__": main()
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