Count of substrings containing only the given character Last Updated : 10 May, 2021 Comments Improve Suggest changes Like Article Like Report 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: 9 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: 6 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++ // 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; } Java // 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 Python3 # 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 C# // 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 JavaScript <script> // Javascript program for the above approach // Function that finds the count // of substrings containing only // character C in the string S function countSubString(S, C) { // To store total count // of substrings var count = 0; // To store count of // consecutive C's var conCount = 0; // Loop through the string for (var i = 0; i < S.length; i++) { var 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 document.write(count); } // Driver Code var S = "geeksforgeeks"; var C = "e"; countSubString(S, C); </script> Output: 6 Time Complexity: O(N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Count of substrings containing only the given character R Rohit_prasad Follow Improve Article Tags : Strings Pattern Searching Searching Mathematical DSA substring +2 More Practice Tags : MathematicalPattern SearchingSearchingStrings Similar Reads Count of substrings which contains a given character K times Given a string consisting of numerical alphabets, a character C and an integer K, the task is to find the number of possible substrings which contains the character C exactly K times. Examples: Input : str = "212", c = '2', k = 1 Output : 4 Possible substrings are {"2", "21", "12", "2"} that contain 9 min read Count of substrings from given Ternary strings containing characters at least once Given string str of size N consisting of only 0, 1, and 2, the task is to find the number of substrings that consists of characters 0, 1, and 2 at least once. Examples: Input: str = "0122"Output: 2Explanation:There exists 2 substrings such that the substrings has characters 0, 1, 2 at least once is 6 min read Count of strings that does not contain any character of a given string Given an array arr containing N strings and a string str, the task is to find the number of strings that do not contain any character of string str. Examples: Input: arr[] = {"abcd", "hijk", "xyz", "ayt"}, str="apple"Output: 2Explanation: "hijk" and "xyz" are the strings that do not contain any char 8 min read Count of sub-strings that do not consist of the given character Given a string str and a character c. The task is to find the number of sub-strings that do not consist of the character c. Examples: Input: str = "baa", c = 'b' Output: 3 The sub-strings are "a", "a" and "aa" Input: str = "ababaa", C = 'b' Output: 5 Approach: Initially take a counter that counts th 12 min read Count of substrings formed using a given set of characters only Given a string str and an array arr[] of K characters, the task is to find the number of substrings of str that contain characters only from the given character array arr[]. Note: The string str and the arr[] contain only lowercase alphabets. Examples: Input: S = "abcb", K = 2, charArray[] = {'a', ' 8 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 Count the sum of count of distinct characters present in all Substrings Given a string S consisting of lowercase English letters of size N where (1 <= N <= 105), the task is to print the sum of the count of distinct characters N where (1 <= N <= 105)in all the substrings. Examples: Input: str = "abbca"Output: 28Explanation: The following are the substrings o 8 min read Count of substrings consisting only of vowels Given a string S, the task is to count all substrings which contain only vowels. Examples: Input: S = "geeksforgeeks" Output: 7 Explanation: Substrings {"e", "ee", "e", "o", "e", "ee", "e"} consists only of vowels. Input: S = "aecui" Output: 6 Explanation: Substrings {"a", "ae", "e", "u", "ui", "i"} 11 min read 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 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 Like