Count of substrings containing only the given character

Given a string S and a character C, the task is to count the number of substrings of S that contains only the character C.
Examples: 
 

Input: S = “0110111”, C = ‘1’ 
Output:
Explanation: 
The substrings containing only ‘1’ are:
 “1” — 5 times 
“11” — 3 times 
“111” — 1 time 
Hence, the count is 9.

Input: S = “geeksforgeeks”, C = ‘e’ 
Output:
 

 

Naive Approach: 
The simplest approach is to generate all possible substrings of the given string S and count the substrings which contains only character C. 
Time Complexity: O(N3
Space Complexity: O(1)
Efficient Approach: 
To optimize the above approach, the fact that a string of length N forms N*(N+1)/2 substrings can be applied. Therefore, for N consecutive occurrence of character C in the string, N*(N+1)/2 substrings are generated. Hence, iterate through the entire length of the string S and for each consecutive substring of character C, count the possible number of substrings possible from them and add to the total count of substrings possible.
 



Illustration: 
S = “0110111”, C = ‘1’ 
 

Below is the implementation of the above approach:
 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that finds the count
// of substrings containing only
// character C in the string S
void countSubString(string S, char C)
{
    // To store total count
    // of substrings
    int count = 0;
  
    // To store count of
    // consecutive C's
    int conCount = 0;
  
    // Loop through the string
    for (char ch : S) {
  
        // Increase the consecutive
        // count of C's
        if (ch == C)
            conCount++;
  
        else {
  
            // Add count of sub-strings
            // from consecutive strings
            count += (conCount
                      * (conCount + 1))
                     / 2;
  
            // Reset the consecutive
            // count of C's
            conCount = 0;
        }
    }
  
    // Add count of sub-strings from
    // consecutive strings
    count += (conCount
              * (conCount + 1))
             / 2;
  
    // Print the count of sub-strings
    // containing only C
    cout << count;
}
  
// Driver Code
int main()
{
    string S = "geeksforgeeks";
  
    char C = 'e';
  
    countSubString(S, C);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach 
import java.util.*;
  
class GFG{
      
// Function that finds the count
// of substrings containing only
// character C in the string S
static void countSubString(String S, char C)
{
      
    // To store total count
    // of substrings
    int count = 0;
  
    // To store count of
    // consecutive C's
    int conCount = 0;
  
    // Loop through the string
    for(int i = 0; i < S.length(); i++)
    {
        char ch = S.charAt(i);
          
        // Increase the consecutive
        // count of C's
        if (ch == C)
            conCount++;
  
        else
        {
              
            // Add count of sub-strings
            // from consecutive strings
            count += (conCount * 
                     (conCount + 1)) / 2;
  
            // Reset the consecutive
            // count of C's
            conCount = 0;
        }
    }
  
    // Add count of sub-strings from
    // consecutive strings
    count += (conCount * 
             (conCount + 1)) / 2;
  
    // Print the count of sub-strings
    // containing only C
    System.out.println(count);
}
  
// Driver Code
public static void main(String s[])
{
    String S = "geeksforgeeks";
    char C = 'e';
      
    countSubString(S, C);
}
  
// This code is contributed by rutvik_56

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
  
# Function that finds the count
# of substrings containing only
# character C in the S
def countSubString(S, C):
  
    # To store total count
    # of substrings
    count = 0
  
    # To store count of
    # consecutive C's
    conCount = 0
  
    # Loop through the string
    for ch in S:
  
        # Increase the consecutive
        # count of C's
        if (ch == C):
            conCount += 1
              
        else:
  
            # Add count of sub-strings
            # from consecutive strings
            count += ((conCount * 
                      (conCount + 1)) // 2)
  
            # Reset the consecutive
            # count of C's
            conCount = 0
  
    # Add count of sub-strings from
    # consecutive strings
    count += ((conCount * 
              (conCount + 1)) // 2)
  
    # Print the count of sub-strings
    # containing only C
    print(count)
  
# Driver Code
if __name__ == '__main__':
    
    S = "geeksforgeeks"
    C = 'e'
  
    countSubString(S, C)
      
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach  
using System;
  
class GFG{
  
// Function that finds the count
// of substrings containing only
// character C in the string S
static void countSubString(String S, char C)
{
  
    // To store total count
    // of substrings
    int count = 0;
  
    // To store count of
    // consecutive C's
    int conCount = 0;
  
    // Loop through the string
    for(int i = 0; i < S.Length; i++)
    {
        char ch = S[i];
  
        // Increase the consecutive
        // count of C's
        if (ch == C)
            conCount++;
  
        else
        {
              
            // Add count of sub-strings
            // from consecutive strings
            count += (conCount * 
                     (conCount + 1)) / 2;
  
            // Reset the consecutive
            // count of C's
            conCount = 0;
        }
    }
  
    // Add count of sub-strings from
    // consecutive strings
    count += (conCount * 
             (conCount + 1)) / 2;
  
    // Print the count of sub-strings
    // containing only C
    Console.Write(count);
}
  
// Driver Code
public static void Main(String[] args)
{
    String S = "geeksforgeeks";
    char C = 'e';
  
    countSubString(S, C);
}
}
  
// This code is contributed by grand_master

chevron_right


Output: 

6

 

Time Complexity: O(N) 
Auxiliary Space: O(1)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.