Count substrings made up of a single distinct character
Last Updated :
28 May, 2021
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: 15
Explanation: All substrings made up of a single distinct character are {"g", "e", "ee", "e", "k", "s", "f", "o", "r", "g", "e", "ee", "e", "k", "s"}.
Input: str = "abaanndscx"
Output: 12
Naive Approach: The simplest approach to solve this problem is to generate all substrings from the given string count the number of substrings that consist of a single distinct character only.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Effective Approach: Follow the steps below to optimize the above approach:
- Initialize a variable, say ans, to store the count of such substrings.
- Iterate over the characters of the string and check if the previous character, say pre, is the same as the current character or not.
- While the previous and current characters are found to be the same, increment subs and add to the answer.
- If the previous and current characters are found to be different, then reinitialize subs to 1.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
// Function to count the number
// of substrings made up of a
// single distinct character
void countSubstrings(string s)
{
// Stores the required count
int ans = 0;
// Stores the count of substrings
// possible by using current character
int subs = 1;
// Stores the previous character
char pre = '0';
// Traverse the string
for (auto& i : s)
{
// If current character is same
// as the previous character
if(pre == i)
{
// Increase count of substrings
// possible with current character
subs += 1;
}
else
{
// Reset count of substrings
// possible with current character
subs = 1;
}
// Update count of substrings
ans += subs;
// Update previous character
pre = i;
}
cout << ans <<endl;
}
// Driver code
int main()
{
string s = "geeksforgeeks";
countSubstrings(s);
return 0;
}
// This code is contributed by splevel62.
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to count the number
// of substrings made up of a
// single distinct character
static void countSubstrings(String s)
{
// Stores the required count
int ans = 0;
// Stores the count of substrings
// possible by using current character
int subs = 1;
// Stores the previous character
char pre = '0';
// Traverse the string
for(char i : s.toCharArray())
{
// If current character is same
// as the previous character
if(pre == i)
{
// Increase count of substrings
// possible with current character
subs += 1;
}
else
{
// Reset count of substrings
// possible with current character
subs = 1;
}
// Update count of substrings
ans += subs;
// Update previous character
pre = i;
}
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
String s = "geeksforgeeks";
countSubstrings(s);
}
}
// This code is contributed by souravghosh0416.
Python3
# Python3 Program to
# implement the above approach
# Function to count the number
# of substrings made up of a
# single distinct character
def countSubstrings(s):
# Stores the required count
ans = 0
# Stores the count of substrings
# possible by using current character
subs = 1
# Stores the previous character
pre = ''
# Traverse the string
for i in s:
# If current character is same
# as the previous character
if pre == i:
# Increase count of substrings
# possible with current character
subs += 1
else:
# Reset count of substrings
# possible with current character
subs = 1
# Update count of substrings
ans += subs
# Update previous character
pre = i
print(ans)
# Driver Code
s = 'geeksforgeeks'
countSubstrings(s)
C#
// C# Program to
// implement the above approach
using System;
class GFG {
// Function to count the number
// of substrings made up of a
// single distinct character
static void countSubstrings(string s)
{
// Stores the required count
int ans = 0;
// Stores the count of substrings
// possible by using current character
int subs = 1;
// Stores the previous character
char pre = '0';
// Traverse the string
foreach(char i in s)
{
// If current character is same
// as the previous character
if(pre == i)
{
// Increase count of substrings
// possible with current character
subs += 1;
}
else
{
// Reset count of substrings
// possible with current character
subs = 1;
}
// Update count of substrings
ans += subs;
// Update previous character
pre = i;
}
Console.WriteLine(ans);
}
// Driver code
static void Main() {
string s = "geeksforgeeks";
countSubstrings(s);
}
}
// This code is contributed by divyesh072019.
JavaScript
<script>
// Javascript Program to implement
// the above approach
// Function to count the number
// of substrings made up of a
// single distinct character
function countSubstrings(s)
{
// Stores the required count
let ans = 0;
// Stores the count of substrings
// possible by using current character
let subs = 1;
// Stores the previous character
let pre = '0';
// Traverse the string
for(let i = 0; i < s.length; i++)
{
// If current character is same
// as the previous character
if(pre == s[i])
{
// Increase count of substrings
// possible with current character
subs += 1;
}
else
{
// Reset count of substrings
// possible with current character
subs = 1;
}
// Update count of substrings
ans += subs;
// Update previous character
pre = s[i];
}
document.write(ans);
}
let s = "geeksforgeeks";
countSubstrings(s);
</script>
Time Complexity: O(N)
Auxiliary Space : O(1)
Similar Reads
Count of substrings having all distinct characters Given a string str consisting of lowercase alphabets, the task is to find the number of possible substrings (not necessarily distinct) that consists of distinct characters only.Examples: Input: Str = "gffg" Output: 6 Explanation: All possible substrings from the given string are, ( "g", "gf", "gff",
7 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 substrings with k distinct characters Given a string s consisting of lowercase characters and an integer k, the task is to count all possible substrings (not necessarily distinct) that have exactly k distinct characters. Examples: Input: s = "abc", k = 2Output: 2Explanation: Possible substrings are ["ab", "bc"]Input: s = "aba", k = 2Out
10 min read
Count of distinct substrings of a string using Suffix Array Given a string of length n of lowercase alphabet characters, we need to count total number of distinct substrings of this string. Examples: Input : str = âababaâ Output : 10 Total number of distinct substring are 10, which are, "", "a", "b", "ab", "ba", "aba", "bab", "abab", "baba" and "ababa"Reco
15+ min read
Count number of substrings having at least K distinct characters Given a string S consisting of N characters and a positive integer K, the task is to count the number of substrings having at least K distinct characters.Examples:Input: S = "abcca", K = 3Output: 4Explanation:The substrings that contain at least K(= 3) distinct characters are:"abc": Count of distinc
7 min read