Count number of substrings of a string consisting of same characters
Last Updated :
22 Dec, 2022
Given a string. The task is to find out the number 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)
{
if (s.empty()) return ;
// 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 contributed by
// sanjeev2552
JavaScript
<script>
// Javascript implementation
// of the above approach
// Function to return the
// number of substrings of
// same characters
function findNumbers(s)
{
// Size of the string
var n = s.length;
// Initialize count to 1
var count = 1;
var result = 0;
// Initialize left to 0 and
// right to 1 to traverse the
// string
var left = 0;
var 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 += parseInt(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 += parseInt(count * (count + 1) / 2);
document.write(result);
}
// Driver code
var s = "bbbcbb";
findNumbers(s);
// This code is contributed by itsok
</script>
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Similar Reads
Maximum count of sub-strings of length K consisting of same characters Given a string str and an integer k. The task is to count the occurrences of sub-strings of length k that consist of the same characters. There can be multiple such sub-strings possible of length k, choose the count of the one which appears the maximum number of times as the sub-string (non-overlapp
6 min read
Count substrings made up of a single distinct character Given a string S of length N, the task is to count the number of substrings made up of a single distinct character.Note: For the repetitive occurrences of the same substring, count all repetitions. Examples: Input: str = "geeksforgeeks"Output: 15Explanation: All substrings made up of a single distin
5 min read
Find distinct characters in distinct substrings of a string Given a string str, the task is to find the count of distinct characters in all the distinct sub-strings of the given string.Examples: Input: str = "ABCA" Output: 18 Distinct sub-stringsDistinct charactersA1AB2ABC3ABCA3B1BC2BCA3C1CA2 Hence, 1 + 2 + 3 + 3 + 1 + 2 + 3 + 1 + 2 = 18Input: str = "AAAB" O
5 min read
Count of substrings of a given Binary string with all characters same Given binary string str containing only 0 and 1, the task is to find the number of sub-strings containing only 1s and 0s respectively, i.e all characters same. Examples: Input: str = â011âOutput: 4Explanation: Three sub-strings are "1", "1", "11" which have only 1 in them, and one substring is there
10 min read
Count substrings consisting of equal number of a, b, c and d Given a string str, the task is to count non-empty substrings with equal number of âaâ, âbâ, âcâ, and âdâ. Examples: Input: str = âabcdefâ Output: 6 Explanation: Substring consisting of equal number of âaâ, âbâ, âcâ, and âdâ are { âabcdâ, âabcdeâ, âabcdfâ, âabcdefâ, âeâ, âfâ }. Therefore, the requir
14 min read