Given a string str and an integer k. The task is to count the occurrences of sub-strings of length k that consist of the same characters. There can be multiple such sub-strings possible of length k, choose the count of the one which appears the maximum number of times as the sub-string (non-overlapping) of str.
Input: str = “aaacaabbaa”, k = 2
“aa” and “bb” are the only sub-strings of length 2 that consist of the same characters.
“bb” appears only once as a sub-string of str whereas “aa” appears thrice (which is the answer)
Input: str = “abab”, k = 2
Approach: Iterate over all the characters from ‘a’ to ‘z’ and count the number of times a string of length k consisting only of the current character appears as a sub-string of str. Print the maximum of these counts in the end.
Below is the implementation of the above approach:
# Python 3 implementation of the approach
# Function to return the count
# of the required sub-strings
def maxSubStrings(s, k):
maxSubStr = 0
n = len(s)
# Iterate over all characters
for c in range(27):
ch = chr(ord(‘a’) + c)
# Count with current character
curr = 0
for i in range(n – k):
if (s[i] != ch):
cnt = 0
while (i < n and s[i] == ch and cnt != k): i += 1 cnt += 1 i -= 1 # If the substring has a length k then # increment count with current character if (cnt == k): curr += 1 # Update max count maxSubStr = max(maxSubStr, curr) return maxSubStr # Driver Code if __name__ == '__main__': s = "aaacaabbaa" k = 2 print(maxSubStrings(s, k)) # This code is contributed by # Surendra_Gangwar [tabby title = "C#"]
Time Complexity: O(n), where n is the length of the string.
- Length of the smallest sub-string consisting of maximum distinct characters
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k
- Count substrings with same first and last characters
- Count distinct substrings that contain some characters at most k times
- Count number of substrings with exactly k distinct characters
- Recursive solution to count substrings with same first and last characters
- Count all Prime Length Palindromic Substrings
- Count number of distinct substrings of a given length
- Maximum length substring having all same characters after k changes
- Maximum length palindrome that can be created with characters in range L and R
- Maximum length of balanced string after swapping and removal of characters
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
- Count maximum-length palindromes in a String
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Queries for frequencies of characters in substrings
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.