Given a string and an integer k, find number of substrings in which all the different characters occurs exactly k times.
Input : s = "aabbcc" k = 2 Output : 6 The substrings are aa, bb, cc, aabb, bbcc and aabbcc. Input : s = "aabccc" k = 2 Output : 3 There are three substrings aa, cc and cc
The idea is to traverse through all substrings. We fix a starting point, traverse through all substrings starring with the picked point, we keep incrementing frequencies of all characters. If all frequencies become k, we increment result. If count of any frequency becomes more than k, we break and change starting point.
Time Complexity : O(n2) where n is length of input string.
This article is contributed by Rahul Chawla. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Count substrings that starts with character X and ends with character Y
- Count all substrings having character K
- Count of substrings which contains a given character K times
- Count substrings with each character occurring at most k times
- Count Substrings with equal number of 0s, 1s and 2s
- Count number of substrings with exactly k distinct characters
- Count number of distinct substrings of a given length
- Count number of substrings with numeric value greater than X
- Given a binary string, count number of substrings that start and end with 1.
- Count the number of vowels occurring in all the substrings of given string
- Count number of substrings of a string consisting of same characters
- Count of number of given string in 2D character array
- Print Kth character in sorted concatenated substrings of a string
- Count substrings with same first and last characters
- Count substrings that contain all vowels | SET 2