Given a string str, a character c and an integer k > 0. The task is to find the number of sub-strings that contain the character c exactly k times.
Input: str = “abada”, c = ‘a’, K = 2
All possible sub-strings are “aba”, “abad”, “bada” and “ada”.
Input: str = “55555”, c = ‘5’, k = 4
Naive approach: A simple solution is to generate all the sub-strings and check whether the count of given character is exactly k times.
Time complexity of this approach is O(n2).
Efficient approach: An efficient solution is to use sliding window technique. Find the sub-string that exactly contains the given character k times starting with character c. Count the number of characters on either side side of the sub-string. Multiply the counts to get the number of possible sub-strings. Time complexity of this approach is O(n).
Below is the implementation of the above approach:
# Python 3 implementation of the approach
# Function to return the count
# of required sub-strings
def countSubString(s, c, k):
# Left and right counters for characters
# on both sides of sub-string window
leftCount = 0
rightCount = 0
# Left and right pointer on both
# sides of sub-string window
left = 0
right = 0
# Initialize the frequency
freq = 0
# Result and length of string
result = 0
len1 = len(s)
# Initialize the left pointer
while (s[left] != c and left < len1): left += 1 leftCount += 1 # Initialize the right pointer right = left + 1 while (freq != (k - 1) and (right - 1) < len1): if (s[right] == c): freq += 1 right += 1 # Traverse all the window sub-strings while (left < len1 and (right - 1) < len1): # Counting the characters on left side # of the sub-string window while (s[left] != c and left < len1): left += 1 leftCount += 1 # Counting the characters on right side # of the sub-string window while (right < len1 and s[right] != c): if (s[right] == c): freq += 1 right += 1 rightCount += 1 # Add the possible sub-strings # on both sides to result result = (result + (leftCount + 1) * (rightCount + 1)) # Setting the frequency for next # sub-string window freq = k - 1 # Reset the left and right counters leftCount = 0 rightCount = 0 left += 1 right += 1 return result # Driver code if __name__ == '__main__': s = "abada" c = 'a' k = 2 print(countSubString(s, c, k)) # This code is contributed by # Surendra_Gangwar [tabby title="C#"]
- Count of substrings which contains a given character K times
- Count substrings with each character occurring at most k times
- Number of substrings with count of each character as k
- Count substrings that starts with character X and ends with character Y
- Replace every character of string by character whose ASCII value is K times more than it
- Count distinct substrings that contain some characters at most k times
- Print Kth character in sorted concatenated substrings of a string
- Longest subsequence where every character appears at-least k times
- Print the string after the specified character has occurred given no. of times
- Encrypt a string by repeating i-th character i times
- Longest subsequence where each character occurs at least k 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
- Number of substrings of a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.