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.
Examples:
Input: str = “aaacaabbaa”, k = 2
Output: 3
“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
Output: 0
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:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the count // of the required sub-strings int maxSubStrings(string s, int k) { int maxSubStr = 0, n = s.size(); // Iterate over all characters for (int c = 0; c < 26; c++) { char ch = 'a' + c; // Count with current character int curr = 0; for (int i = 0; i <= n - k; i++) { if (s[i] != ch) continue; int cnt = 0; while (i < n && s[i] == ch && cnt != k) { i++; cnt++; } i--; // If the substring has a length k // then increment count with current character if (cnt == k) curr++; } // Update max count maxSubStr = max(maxSubStr, curr); } return maxSubStr; } // Driver Code int main() { string s = "aaacaabbaa"; int k = 2; cout << maxSubStrings(s, k); return 0; } |
Java
// Java implementation of the approach import java.util.*; import java.lang.*; import java.io.*; class GFG { // Function to return the count // of the required sub-strings static int maxSubStrings(String s, int k) { int maxSubStr = 0, n = s.length(); // Iterate over all characters for (int c = 0; c < 26; c++) { char ch = (char)((int)'a' + c); // Count with current character int curr = 0; for (int i = 0; i <= n - k; i++) { if (s.charAt(i) != ch) continue; int cnt = 0; while (i < n && s.charAt(i) == ch && cnt != k) { i++; cnt++; } i--; // If the substring has a length // k then increment count with // current character if (cnt == k) curr++; } // Update max count maxSubStr = Math.max(maxSubStr, curr); } return maxSubStr; } // Driver Code public static void main(String []args) { String s = "aaacaabbaa"; int k = 2; System.out.println(maxSubStrings(s, k)); } } // This code is contributed by // tufan_gupta2000 |
Python3
# 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): continue 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 |
C#
// C# implementation of the approach using System; class GFG { // Function to return the count // of the required sub-strings static int maxSubStrings(String s, int k) { int maxSubStr = 0, n = s.Length; // Iterate over all characters for (int c = 0; c < 26; c++) { char ch = (char)((int)'a' + c); // Count with current character int curr = 0; for (int i = 0; i <= n - k; i++) { if (s[i] != ch) continue; int cnt = 0; while (i < n && s[i] == ch && cnt != k) { i++; cnt++; } i--; // If the substring has a length // k then increment count with // current character if (cnt == k) curr++; } // Update max count maxSubStr = Math.Max(maxSubStr, curr); } return maxSubStr; } // Driver Code public static void Main() { string s = "aaacaabbaa"; int k = 2; Console.WriteLine(maxSubStrings(s, k)); } } // This code is contributed by Ryuga |
3
Time Complexity: O(n), where n is the length of the string.
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.
Recommended Posts:
- Count number of substrings of a string consisting of same characters
- Maximum length of subarray consisting of same type of element on both halves of sub-array
- Length of the smallest sub-string consisting of maximum distinct characters
- Count of substrings consisting only of vowels
- Count of substrings consisting of even number of vowels
- Count substrings with same first and last characters
- Recursive solution to count substrings with same first and last characters
- Count of substrings of a given Binary string with all characters same
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Count of substrings of length K with exactly K distinct characters
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Count of binary strings of given length consisting of at least one 1
- Maximum length substring having all same characters after k changes
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k
- Calculate Sum of ratio of special characters to length of substrings of the given string
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
- Length of largest subsequence consisting of a pair of alternating digits
- Check whether two strings contain same characters in same order
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

