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 Ktimes.
Examples:
Input : str = "212", c = '2', k = 1
Output : 4
Possible substrings are {"2", "21", "12", "2"}
that contains 2 exactly 1 time.
Input : str = "55555", c = '5', k = 4
Output : 2
Possible substrings are {"5555", "5555"}
that contains 5 exactly 4 times
Naive Approach: A simple solution is to generate all the substrings of string and count the number of substrings in which given character occurs exactly k times.
The time complexity of this solution is O(N2) where N is the length of the string.
Efficient Approach: An efficient solution is to use sliding window technique. Find the substring that contains character C exactly K times starting with character C. Count the number of characters on either side of the substring. Multiply the counts to get the number of possible substrings with given substring
Below is the implementation of the above approach:
C++
// C++ program to count the number of substrings // which contains the character C exactly K times #include <bits/stdc++.h> using namespace std; // Function to count the number of substrings // which contains the character C exactly K times int countSubString(string s, char c, int k) { // left and right counters for characters on // both sides of substring window int leftCount = 0, rightCount = 0; // left and right pointer on both sides // of substring window int left = 0, right = 0; // intialize the frequency int freq = 0; // result and length of string int result = 0, len = s.length(); // initialize the left pointer while (s[left] != c && left < len) { left++; leftCount++; } // initialize the right pointer right = left + 1; while (freq != (k - 1) && (right - 1) < len) { if (s[right] == c) freq++; right++; } // traverse all the window substrings while (left < len && (right - 1) < len) { // counting the characters on leftSide // of substring window while (s[left] != c && left < len) { left++; leftCount++; } // counting the characters on rightSide of // substring window while (right < len && s[right] != c) { if (s[right] == c) freq++; right++; rightCount++; } // Add the possible substrings on both // sides to result result = result + (leftCount + 1) * (rightCount + 1); // Setting the frequency for next // substring window freq = k - 1; // reset the left, right counters leftCount = 0; rightCount = 0; left++; right++; } return result; } // Driver code int main() { string s = "3123231"; char c = '3'; int k = 2; cout << countSubString(s, c, k); return 0; } |
Java
// Java program to count the number of substrings // which contains the character C exactly K times class GFG { // Function to count the number of substrings // which contains the character C exactly K times static int countSubString(String s, char c, int k) { // left and right counters for characters on // both sides of substring window int leftCount = 0, rightCount = 0; // left and right pointer on both sides // of substring window int left = 0, right = 0; // intialize the frequency int freq = 0; // result and length of string int result = 0, len = s.length(); // initialize the left pointer while (s.charAt(left) != c && left < len) { left++; leftCount++; } // initialize the right pointer right = left + 1; while (freq != (k - 1) && (right - 1) < len) { if (s.charAt(right) == c) { freq++; } right++; } // traverse all the window substrings while (left < len && (right - 1) < len) { // counting the characters on leftSide // of substring window while (s.charAt(left) != c && left < len) { left++; leftCount++; } // counting the characters on rightSide of // substring window while (right < len && s.charAt(right) != c) { if (s.charAt(right) == c) { freq++; } right++; rightCount++; } // Add the possible substrings on both // sides to result result = result + (leftCount + 1) * (rightCount + 1); // Setting the frequency for next // substring window freq = k - 1; // reset the left, right counters leftCount = 0; rightCount = 0; left++; right++; } return result; } // Driver code public static void main(String args[]) { String s = "3123231"; char c = '3'; int k = 2; System.out.println(countSubString(s, c, k)); } } /* This code is contributed by PrinciRaj1992 */ |
Python3
# Python3 program to count the number of substrings
# which contains the character C exactly K times
# Function to count the number of substrings
# which contains the character C exactly K times
def countSubString(s, c, k):
# left and right counters for characters
# on both sides of subwindow
leftCount = 0
rightCount = 0
# left and right poer on both sides
# of subwindow
left = 0
right = 0
# ialize the frequency
freq = 0
# result and Length of string
result = 0
Len = len(s)
# initialize the left poer
while (s[left] != c and left < Len):
left += 1
leftCount += 1
# initialize the right poer
right = left + 1
while (freq != (k - 1) and (right - 1) < Len):
if (s[right] == c):
freq += 1
right += 1
# traverse all the window substrings
while (left < Len and (right - 1) < Len):
# counting the characters on leftSide
# of subwindow
while (s[left] != c and left < Len):
left += 1
leftCount += 1
# counting the characters on rightSide of
# subwindow
while (right < Len and s[right] != c):
if (s[right] == c):
freq += 1
right += 1
rightCount += 1
# Add the possible substrings on both
# sides to result
result = (result + (leftCount + 1) *
(rightCount + 1))
# Setting the frequency for next
# subwindow
freq = k - 1
# reset the left, right counters
leftCount = 0
rightCount = 0
left += 1
right += 1
return result
# Driver code
s = "3123231"
c = '3'
k = 2
print(countSubString(s, c, k))
# This code is contributed by Mohit Kumar
[tabby title = "C#"]
// C# program to count the number of substrings // which contains the character C exactly K times using System; class GFG { // Function to count the number of substrings // which contains the character C exactly K times static int countSubString(string s, char c, int k) { // left and right counters for characters on // both sides of substring window int leftCount = 0, rightCount = 0; // left and right pointer on both sides // of substring window int left = 0, right = 0; // intialize the frequency int freq = 0; // result and length of string int result = 0, len = s.Length; // initialize the left pointer while (s[left] != c && left < len) { left++; leftCount++; } // initialize the right pointer right = left + 1; while (freq != (k - 1) && (right - 1) < len) { if (s[right] == c) freq++; right++; } // traverse all the window substrings while (left < len && (right - 1) < len) { // counting the characters on leftSide // of substring window while (s[left] != c && left < len) { left++; leftCount++; } // counting the characters on rightSide of // substring window while (right < len && s[right] != c) { if (s[right] == c) freq++; right++; rightCount++; } // Add the possible substrings on both // sides to result result = result + (leftCount + 1) * (rightCount + 1); // Setting the frequency for next // substring window freq = k - 1; // reset the left, right counters leftCount = 0; rightCount = 0; left++; right++; } return result; } // Driver code static public void Main () { string s = "3123231"; char c = '3'; int k = 2; Console.WriteLine(countSubString(s, c, k)); } } // This code is contributed by AnkitRai01 |
8
Time Complexity: O(N)
Recommended Posts:
- Count substrings with each character occurring at most k times
- Count substrings that starts with character X and ends with character Y
- Count distinct substrings that contain some characters at most k times
- Number of substrings with count of each character as k
- Replace every character of string by character whose ASCII value is K times more than it
- Repeat substrings of the given String required number of times
- Print Kth character in sorted concatenated substrings of a string
- Number of sub-strings that contain the given character exactly k times
- Longest subsequence where each character occurs at least k times
- Print the string after the specified character has occurred given no. of times
- Longest subsequence where every character appears at-least k times
- Encrypt a string by repeating i-th character i times
- Check if max occurring character of one string appears same no. of times in other
- Decrypt a string encrypted by repeating i-th character i times
- Lexicographically largest subsequence such that every character occurs at least k times
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.



