Count all substrings having character K
Given a string str and a character K, the task is to find the count of all the substrings of str that contain the character K.
Examples:
Input: str = “geeks”, K = ‘g’
Output: 5
“g”, “ge”, “gee”, “geek” and “geeks” are the valid substrings.
Input: str = “geeksforgeeks”, K = ‘k’
Output: 56
Naive approach A simple approach will be to find all the substrings having character K of the given string and return the count;
Efficient approach: For every index i in the string, find the first index j such that i ≤ j and str[j] = K. Now, the substrings str[i…j], str[i…j + 1], str[i…j + 2], …, str[i…n – 1] will all contain the character K at least once. The approach seems to be O(n2) at first but the index j will not be calculated again for every index i, j will be a valid index for all the values of i less than j.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the index of the // next occurrence of character ch in str // starting from the given index int nextOccurrence(string str, int n, int start, char ch) { for (int i = start; i < n; i++) { // Return the index of the first // occurrence of ch if (str[i] == ch) return i; } // No occurrence found return -1; } // Function to return the count of all // the substrings of str which contain // the character ch at least one int countSubStr(string str, int n, char ch) { // To store the count of valid substrings int cnt = 0; // Index of the first occurrence of ch in str int j = nextOccurrence(str, n, 0, ch); for (int i = 0; i < n; i++) { while (j != -1 && j < i) { j = nextOccurrence(str, n, j + 1, ch); } // No occurrence of ch after index i in str if (j == -1) break; // Substrings starting at index i // and ending at indices j, j+1, ..., n-1 // are all valid substring cnt += (n - j); } return cnt; } // Driver code int main() { string str = "geeksforgeeks"; int n = str.length(); char ch = 'k'; cout << countSubStr(str, n, ch); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the index of the // next occurrence of character ch in str // starting from the given index static int nextOccurrence(String str, int n, int start, char ch) { for (int i = start; i < n; i++) { // Return the index of the first // occurrence of ch if (str.charAt(i) == ch) return i; } // No occurrence found return -1; } // Function to return the count of all // the substrings of str which contain // the character ch at least one static int countSubStr(String str, int n, char ch) { // To store the count of valid substrings int cnt = 0; // Index of the first occurrence of ch in str int j = nextOccurrence(str, n, 0, ch); for (int i = 0; i < n; i++) { while (j != -1 && j < i) { j = nextOccurrence(str, n, j + 1, ch); } // No occurrence of ch after index i in str if (j == -1) break; // Substrings starting at index i // and ending at indices j, j+1, ..., n-1 // are all valid substring cnt += (n - j); } return cnt; } // Driver code static public void main ( String []arg) { String str = "geeksforgeeks"; int n = str.length(); char ch = 'k'; System.out.println(countSubStr(str, n, ch)); } } // This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation of the approach # Function to return the index of the # next occurrence of character ch in strr # starting from the given index def nextOccurrence(strr, n, start, ch): for i in range(start, n): # Return the index of the first # occurrence of ch if (strr[i] == ch): return i # No occurrence found return -1 # Function to return the count of all # the substrrings of strr which contain # the character ch at least one def countSubStr(strr, n, ch): # To store the count of valid substrrings cnt = 0 # Index of the first occurrence of ch in strr j = nextOccurrence(strr, n, 0, ch) for i in range(n): while (j != -1 and j < i): j = nextOccurrence(strr, n, j + 1, ch) # No occurrence of ch after index i in strr if (j == -1): break # Substrrings starting at index i # and ending at indices j, j+1, ..., n-1 # are all valid substrring cnt += (n - j) return cnt # Driver code strr = "geeksforgeeks"n = len(strr) ch = 'k' print(countSubStr(strr, n, ch)) # This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach using System; class GFG { // Function to return the index of the // next occurrence of character ch in str // starting from the given index static int nextOccurrence(String str, int n, int start, char ch) { for (int i = start; i < n; i++) { // Return the index of the first // occurrence of ch if (str[i] == ch) return i; } // No occurrence found return -1; } // Function to return the count of all // the substrings of str which contain // the character ch at least one static int countSubStr(String str, int n, char ch) { // To store the count of valid substrings int cnt = 0; // Index of the first occurrence of ch in str int j = nextOccurrence(str, n, 0, ch); for (int i = 0; i < n; i++) { while (j != -1 && j < i) { j = nextOccurrence(str, n, j + 1, ch); } // No occurrence of ch after index i in str if (j == -1) break; // Substrings starting at index i // and ending at indices j, j+1, ..., n-1 // are all valid substring cnt += (n - j); } return cnt; } // Driver code static public void Main () { String str = "geeksforgeeks"; int n = str.Length; char ch = 'k'; Console.WriteLine(countSubStr(str, n, ch)); } } // This code is contributed by AnkitRai01 |
56
Recommended Posts:
- Count substrings that starts with character X and ends with character Y
- Count of substrings which contains a given character K times
- Number of substrings with count of each character as k
- Count substrings with each character occurring at most k times
- Print Kth character in sorted concatenated substrings of a string
- Find the count of M character words which have at least one character repeated
- Count substrings with same first and last characters
- Count substrings that contain all vowels | SET 2
- Count Substrings with equal number of 0s, 1s and 2s
- Count of total anagram substrings
- Count of substrings of a binary string containing K ones
- Count number of substrings with exactly k distinct characters
- Recursive solution to count substrings with same first and last characters
- Count distinct substrings that contain some characters at most k times
- Find the count of substrings in alphabetic order
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.



