Given a string ‘str’ and an integer ‘k’, the task is to count the number of sub-strings of length ‘k’ which are comprised of the same character. Given string contains only lowercase alphabets.
Examples:
Input: str = "aaaabbbccdddd", k=4 Output: 2 The sub-strings of length 4 which contain identical characters are 'aaaa' and 'dddd'. So, the count is 2. Input: str = "aaaaaa", k=4 Output: 3
A simple approach:
- Find all the sub-strings of the string that are of length ‘k’.
- Check if those sub-strings are composed of only ‘1’ character.
- If the above conditions hold then increase the count.
- Display the count.
Efficient approach: We will use Window sliding technique to solve this problem.
- Take a sub-string of length ‘k’ (which is the first ‘k’ characters).
- Then, add next character to the sub-string.
- If the length of the sub-string is greater than ‘k’ then remove the character from the beginning of the string.
- And, count the frequency of the characters in the sub-string with the help of a map.
- If the frequency of one of the sub-string’s character is equal to length of the sub-string itself i.e. all the characters are same.
- Then, increase the count else repeat the steps above until the there’s no more character to add in the end.
- Display the total count in the end.
Below is the implementation of the above approach :
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function that counts all the // sub-strings of length 'k' // which have all identical characters void solve(string s, int k) { // count of sub-strings, length, // initial position of sliding window int count = 0, length = 0, pos = 0; // map to store the frequency of // the characters of sub-string map<char, int> m; for (int i = 0; i < s.length(); i++) { // increase the frequency of the character // and length of the sub-string m[s[i]]++; length++; // if the length of the sub-string // is greater than K if (length > k) { // remove the character from // the beginning of sub-string m[s[pos++]]--; length--; } // if the length of the sub string // is equal to k and frequency of one // of its characters is equal to the // length of the sub-string // i.e. all the characters are same // increase the count if (length == k && m[s[i]] == length) count++; } // display the number // of valid sub-strings cout << count << endl; } // Driver code int main() { string s = "aaaabbbccdddd"; int k = 4; solve(s, k); return 0; } |
chevron_right
filter_none
Java
// Java implementation of above approach import java.util.*; class GFG { // Function that counts all the // sub-strings of length 'k' // which have all identical characters static void solve(String s, int k) { // count of sub-strings, length, // initial position of sliding window int count = 0, length = 0, pos = 0; // map to store the frequency of // the characters of sub-string HashMap<Character, Integer> m = new HashMap<Character, Integer>(); for (int i = 0; i < s.length(); i++) { // increase the frequency of the character // and length of the sub-string if(m.containsKey(s.charAt(i))) m.put(s.charAt(i), m.get(s.charAt(i))+1); else m.put(s.charAt(i), 1); length++; // if the length of the sub-string // is greater than K if (length > k) { // remove the character from // the beginning of sub-string m.put(s.charAt(pos), m.get(s.charAt(pos)) -1 ); pos++; length--; } // if the length of the sub string // is equal to k and frequency of one // of its characters is equal to the // length of the sub-string // i.e. all the characters are same // increase the count if (length == k && m.get(s.charAt(i)) == length) count++; } // display the number // of valid sub-strings System.out.println( count); } // Driver code public static void main (String[] args) { String s = "aaaabbbccdddd"; int k = 4; solve(s, k); } } // This code is contributed by ihritik |
chevron_right
filter_none
Python 3
# Python3 implementation of above # approach # Function that counts all the # sub-strings of length 'k' # which have all identical characters def solve(s, k) : # count of sub-strings, length, # initial position of sliding window count, length, pos = 0, 0, 0 # dictionary to store the frequency of # the characters of sub-string m = dict.fromkeys(s,0) for i in range(len(s)) : # increase the frequency of the character # and length of the sub-string m[s[i]] += 1 length += 1 # if the length of the sub-string # is greater than K if length > k : # remove the character from # the beginning of sub-string m[s[pos]] -= 1 pos += 1 length -= 1 # if the length of the sub string # is equal to k and frequency of one # of its characters is equal to the # length of the sub-string # i.e. all the characters are same # increase the count if length == k and m[s[i]] == length : count += 1 # display the number # of valid sub-strings print(count) # Driver code if __name__ == "__main__" : s = "aaaabbbccdddd" k = 4 # Function call solve(s, k) # This code is contributed by # ANKITRAI1 |
chevron_right
filter_none
C#
// C# implementation of above approach using System; using System.Collections.Generic; class GFG { // Function that counts all the // sub-strings of length 'k' // which have all identical characters static void solve(string s, int k) { // count of sub-strings, length, // initial position of sliding window int count = 0, length = 0, pos = 0; // map to store the frequency of // the characters of sub-string Dictionary<char, int> m = new Dictionary<char, int>(); for (int i = 0; i < s.Length; i++) { // increase the frequency of the character // and length of the sub-string if(m.ContainsKey(s[i])) m[s[i]]++; else m[s[i]] = 1; length++; // if the length of the sub-string // is greater than K if (length > k) { // remove the character from // the beginning of sub-string m[s[pos]]--; pos++; length--; } // if the length of the sub string // is equal to k and frequency of one // of its characters is equal to the // length of the sub-string // i.e. all the characters are same // increase the count if (length == k && m[s[i]] == length) count++; } // display the number // of valid sub-strings Console.WriteLine(count); } // Driver code public static void Main () { string s = "aaaabbbccdddd"; int k = 4; solve(s, k); } } // This code is contributed by ihritik |
chevron_right
filter_none
PHP
<?php // PHP implementation of above approach // Function that counts all the // sub-strings of length 'k' // which have all identical characters function solve($s, $k) { // count of sub-strings, length, // initial position of sliding window $count = 0; $length = 0; $pos = 0; // map to store the frequency of // the characters of sub-string $m = array(); for ($i = 0; $i < strlen($s); $i++) { // increase the frequency of the character // and length of the sub-string $m[$s[$i]]++; $length++; // if the length of the sub-string // is greater than K if ($length > $k) { // remove the character from // the beginning of sub-string $m[$s[$pos++]]--; $length--; } // if the length of the sub string // is equal to k and frequency of one // of its characters is equal to the // length of the sub-string // i.e. all the characters are same // increase the count if ($length == $k && $m[$s[$i]] == $length) $count++; } // display the number // of valid sub-strings echo $count . "\n"; } // Driver code $s = "aaaabbbccdddd"; $k = 4; solve($s, $k); // This code is contributed // by ChitraNayal ?> |
chevron_right
filter_none
Output:
2
Recommended Posts:
- Minimize length of Substrings containing at least one common Character
- Count of substrings containing only the given character
- Count substrings that starts with character X and ends with character Y
- Count of substrings of a binary string containing K ones
- Count of substrings of a Binary string containing only 1s
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Map every character of one string to another such that all occurrences are mapped to the same character
- Count of strings possible by replacing two consecutive same character with new character
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Length of longest common subsequence containing vowels
- Count of subsequences of length atmost K containing distinct prime elements
- Lexicographically smallest K-length substring containing maximum number of vowels
- Smallest number containing all possible N length permutations using digits 0 to D
- Minimize length of prefix of string S containing all characters of another string T
- Print Kth character in sorted concatenated substrings of a string
- Number of substrings with count of each character as k
- 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
- Maximize partitions such that no two substrings have any common character
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.


