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++
#include <bits/stdc++.h>
using namespace std;
void findNumbers(string s)
{
if (s.empty()) return ;
int n = s.size();
int count = 1;
int result = 0;
int left = 0;
int right = 1;
while (right < n) {
if (s[left] == s[right]) {
count++;
}
else {
result += count * (count + 1) / 2;
left = right;
count = 1;
}
right++;
}
result += count * (count + 1) / 2;
cout << result << endl;
}
int main()
{
string s = "bbbcbb" ;
findNumbers(s);
}
|
Java
class GFG
{
static void findNumbers(String s)
{
int n = s.length();
int count = 1 ;
int result = 0 ;
int left = 0 ;
int right = 1 ;
while (right < n)
{
if (s.charAt(left) == s.charAt(right))
{
count++;
}
else
{
result += count * (count + 1 ) / 2 ;
left = right;
count = 1 ;
}
right++;
}
result += count * (count + 1 ) / 2 ;
System.out.println(result);
}
public static void main (String[] args)
{
String s = "bbbcbb" ;
findNumbers(s);
}
}
|
Python3
def findNumbers(s):
n = len (s)
count = 1
result = 0
left = 0
right = 1
while (right < n):
if (s[left] = = s[right]):
count + = 1
else :
result + = count * (count + 1 ) / / 2
left = right
count = 1
right + = 1
result + = count * (count + 1 ) / / 2
print (result)
s = "bbbcbb"
findNumbers(s)
|
C#
using System;
class GFG
{
static void findNumbers(String s)
{
int n = s.Length;
int count = 1;
int result = 0;
int left = 0;
int right = 1;
while (right < n)
{
if (s[left] == s[right])
count++;
else
{
result += count * (count + 1) / 2;
left = right;
count = 1;
}
right++;
}
result += count * (count + 1) / 2;
Console.WriteLine(result);
}
public static void Main(String[] args)
{
String s = "bbbcbb" ;
findNumbers(s);
}
}
|
Javascript
<script>
function findNumbers(s)
{
var n = s.length;
var count = 1;
var result = 0;
var left = 0;
var right = 1;
while (right < n)
{
if (s[left] == s[right])
{
count++;
}
else
{
result += parseInt(count * (count + 1) / 2);
left = right;
count = 1;
}
right++;
}
result += parseInt(count * (count + 1) / 2);
document.write(result);
}
var s = "bbbcbb" ;
findNumbers(s);
</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.
Share your thoughts in the comments
Please Login to comment...