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 firstname.lastname@example.org. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Return maximum occurring character in an input string
- Given a string, find its first non-repeating character
- Given a number, find the next smallest palindrome
- Count words in a given string
- Find the first non-repeating character from a stream of characters
- Find Excel column name from a given column number
- How to turn off a particular bit in a number?
- Given a binary string, count number of substrings that start and end with 1.
- Factorial of a large number
- Function to find Number of customers who could not get a computer
- Build Lowest Number by Removing n digits from a given number
- Converting Decimal Number lying between 1 to 3999 to Roman Numerals
- Transform One String to Another using Minimum Number of Given Operation
- Minimum number of bracket reversals needed to make an expression balanced
- Form minimum number from given sequence
- 1's and 2's complement of a Binary Number
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Find all strings formed from characters mapped to digits of a number
- All combinations of strings that can be used to dial a number
- K'th Non-repeating Character