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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count substrings that starts with character X and ends with character Y
- Count substrings with each character occurring at most k times
- Count of substrings which contains a given character K times
- Count all substrings having character K
- Count of substrings containing only the given character
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of strings that can be formed from another string using each character at-most once
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Print Kth character in sorted concatenated substrings of a string
- Maximize partitions such that no two substrings have any common character
- Minimize length of Substrings containing at least one common Character
- Replace two substrings (of a string) with each other
- Reverse substrings between each pair of parenthesis
- Count ways to split a Binary String into three substrings having equal count of zeros
- Print number of words, vowels and frequency of each character
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Given a binary string, count number of substrings that start and end with 1.
- Count number of substrings with exactly k distinct characters
- Count Substrings with equal number of 0s, 1s and 2s
- Count number of substrings with numeric value greater than X