Count number of substrings of a string consisting of same characters
Given a string. The task is to find out the numbers of substrings consisting of the same characters.
Examples:
Input: abba
Output: 5
The desired substrings are {a}, {b}, {b}, {a}, {bb}
Input: bbbcbb
Output: 10
Approach : It is known for a string of length n, there are a total of n*(n+1)/2 number of substrings.
Let’s initialize the result to 0. Traverse the string and find the number of consecutive element(let’s say count) of same characters. Whenever we find another character, increment the result by count*(count+1)/2, set count to 1 and from that index, repeat the above process.
Remember, for each different character, the number of our desired substring is 1.
Below is the implementation of the above approach:
C++
// C++ implementation // of the above approach #include <bits/stdc++.h> using namespace std; // Function to return the // number of substrings of // same characters void findNumbers(string s) { // Size of the string int n = s.size(); // Initialize count to 1 int count = 1; int result = 0; // Initialize left to 0 and // right to 1 to traverse the // string int left = 0; int right = 1; while (right < n) { // Checking if consecutive // characters are same and // increment the count if (s[left] == s[right]) { count++; } // When we encounter a // different characters else { // Increment the result result += count * (count + 1) / 2; // To repeat the whole // process set left equals // right and count variable to 1 left = right; count = 1; } right++; } // Store the final // value of result result += count * (count + 1) / 2; cout << result << endl; } // Driver code int main() { string s = "bbbcbb"; findNumbers(s); } |
Java
// Java implementation of the approach class GFG { // Function to return the // number of substrings of // same characters static void findNumbers(String s) { // Size of the string int n = s.length(); // Initialize count to 1 int count = 1; int result = 0; // Initialize left to 0 and // right to 1 to traverse the // string int left = 0; int right = 1; while (right < n) { // Checking if consecutive // characters are same and // increment the count if (s.charAt(left) == s.charAt(right)) { count++; } // When we encounter a // different characters else { // Increment the result result += count * (count + 1) / 2; // To repeat the whole // process set left equals // right and count variable to 1 left = right; count = 1; } right++; } // Store the final // value of result result += count * (count + 1) / 2; System.out.println(result); } // Driver code public static void main (String[] args) { String s = "bbbcbb"; findNumbers(s); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the above approach # Function to return the number of # substrings of same characters def findNumbers(s): # Size of the string n = len(s) # Initialize count to 1 count = 1 result = 0 # Initialize left to 0 and right to 1 # to traverse the string left = 0 right = 1 while (right < n): # Checking if consecutive # characters are same and # increment the count if (s[left] == s[right]): count += 1 # When we encounter a # different characters else: # Increment the result result += count * (count + 1) // 2 # To repeat the whole # process set left equals # right and count variable to 1 left = right count = 1 right += 1 # Store the final value of result result += count * (count + 1) // 2 print(result) # Driver code s = "bbbcbb" findNumbers(s) # This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach using System; class GFG { // Function to return the // number of substrings of // same characters static void findNumbers(String s) { // Size of the string int n = s.Length; // Initialize count to 1 int count = 1; int result = 0; // Initialize left to 0 and // right to 1 to traverse the // string int left = 0; int right = 1; while (right < n) { // Checking if consecutive // characters are same and // increment the count if (s[left] == s[right]) count++; // When we encounter a // different characters else { // Increment the result result += count * (count + 1) / 2; // To repeat the whole // process set left equals // right and count variable to 1 left = right; count = 1; } right++; } // Store the final // value of result result += count * (count + 1) / 2; Console.WriteLine(result); } // Driver code public static void Main(String[] args) { String s = "bbbcbb"; findNumbers(s); } } // This code is contibuted by // sanjeev2552 |
10
Recommended Posts:
- Count number of substrings with exactly k distinct characters
- Maximum count of sub-strings of length K consisting of same characters
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
- Length of the smallest sub-string consisting of maximum distinct characters
- Given a binary string, count number of substrings that start and end with 1.
- Count the number of vowels occurring in all the substrings of given string
- Count substrings with same first and last characters
- Recursive solution to count substrings with same first and last characters
- Count distinct substrings that contain some characters at most k times
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k
- Count of substrings of a binary string containing K ones
- Count of distinct substrings of a string using Suffix Array
- Queries to find the count of vowels in the substrings of the given string
- Count of distinct substrings of a string using Suffix Trie
- Permutation of a string with maximum number of characters greater than its adjacent characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

