Count substrings with each character occurring at most k times - GeeksforGeeks

07 May, 2021
Given a string S. Count number of substrings in which each character occurs at most k times. Assume that the string consists of only lowercase English alphabets.Examples:  
Input : S = ab
        k = 1
Output : 3
All the substrings a, b, ab have
individual character count less than 1. 

Input : S = aaabb
        k = 2
Output : 12
Substrings that have individual character
count at most 2 are: a, a, a, b, b, aa, aa,
ab, bb, aab, abb, aabb.
 
A simple solution is to first find all the substrings and then check if count of each character is at most k in each substring. Time complexity of this solution is O(n^3).An efficient solution is to maintain starting and ending point of substrings. Let us fix the starting point to an index i. Keep incrementing the ending point j one at a time. When changing the ending point update the count of corresponding character. Then check for this substring that whether each character has count at most k or not. If yes then increment answer by 1 else increment the starting point and reset ending point. The starting point is incremented because during last update on ending point character count exceed k and it will only increase further. So no subsequent substring with given fixed starting point will be a substring with each character count at most k. Implementation:  
C++
Java
Python 3
C#
Javascript
// CPP program to count number of substrings// in which each character has count less// than or equal to k.#include <bits/stdc++.h> using namespace std; int findSubstrings(string s, int k){    // variable to store count of substrings.    int ans = 0;     // array to store count of each character.    int cnt[26];     int i, j, n = s.length();    for (i = 0; i < n; i++) {         // Initialize all characters count to zero.        memset(cnt, 0, sizeof(cnt));         for (j = i; j < n; j++) {            // increment character count            cnt[s[j] - 'a']++;             // check only the count of current character            // because only the count of this            // character is changed. The ending point is            // incremented to current position only if            // all other characters have count at most            // k and hence their count is not checked.            // If count is less than k, then increase ans            // by 1.            if (cnt[s[j] - 'a'] <= k)                ans++;             // if count is less than k, then break as            // subsequent substrings for this starting            // point will also have count greater than            // k and hence are reduntant to check.            else                break;        }    }     // return the final count of substrings.    return ans;} // Driver codeint main(){    string S = "aaabb";    int k = 2;    cout << findSubstrings(S, k);    return 0;}
import java.util.Arrays; // Java program to count number of substrings// in which each character has count less// than or equal to k.class GFG {     static int findSubstrings(String s, int k) {        // variable to store count of substrings.        int ans = 0;         // array to store count of each character.        int cnt[] = new int[26];         int i, j, n = s.length();        for (i = 0; i < n; i++) {             // Initialize all characters count to zero.            Arrays.fill(cnt, 0);             for (j = i; j < n; j++) {                // increment character count                cnt[s.charAt(j) - 'a']++;                 // check only the count of current character                // because only the count of this                // character is changed. The ending point is                // incremented to current position only if                // all other characters have count at most                // k and hence their count is not checked.                // If count is less than k, then increase ans                // by 1.                if (cnt[s.charAt(j) - 'a'] <= k) {                    ans++;                } // if count is less than k, then break as                // subsequent substrings for this starting                // point will also have count greater than                // k and hence are reduntant to check.                else {                    break;                }            }        }         // return the final count of substrings.        return ans;    } // Driver code    static public void main(String[] args) {        String S = "aaabb";        int k = 2;        System.out.println(findSubstrings(S, k));    }} // This code is contributed by 29AjayKumar
# Python 3 program to count number of substrings# in which each character has count less# than or equal to k. def findSubstrings(s, k):     # variable to store count of substrings.    ans = 0    n = len(s)    for i in range(n):                 # array to store count of each character.        cnt = [0] * 26         for j in range(i, n):                         # increment character count            cnt[ord(s[j]) - ord('a')] += 1             # check only the count of current character            # because only the count of this            # character is changed. The ending point is            # incremented to current position only if            # all other characters have count at most            # k and hence their count is not checked.            # If count is less than k, then increase            # ans by 1.                         if (cnt[ord(s[j]) - ord('a')] <= k):                ans += 1             # if count is less than k, then break as            # subsequent substrings for this starting            # point will also have count greater than            # k and hence are reduntant to check.            else:                break     # return the final count of substrings.    return ans # Driver codeif __name__ == "__main__":         S = "aaabb"    k = 2    print(findSubstrings(S, k)) # This code is contributed by ita_c
// C# program to count number of substrings// in which each character has count less// than or equal to k.using System; class GFG{     public static int findSubstrings(string s, int k){    // variable to store count of substrings.    int ans = 0;     // array to store count of each character.    int []cnt = new int[26];     int i, j, n = s.Length;    for (i = 0; i < n; i++)    {         // Initialize all characters count to zero.        Array.Clear(cnt, 0, cnt.Length);         for (j = i; j < n; j++)        {                         // increment character count            cnt[s[j] - 'a']++;             // check only the count of current character            // because only the count of this            // character is changed. The ending point is            // incremented to current position only if            // all other characters have count at most            // k and hence their count is not checked.            // If count is less than k, then increase ans            // by 1.            if (cnt[s[j] - 'a'] <= k)                ans++;             // if count is less than k, then break as            // subsequent substrings for this starting            // point will also have count greater than            // k and hence are reduntant to check.            else                break;        }    }     // return the final count of substrings.    return ans;} // Driver codepublic static int Main(){    string S = "aaabb";    int k = 2;    Console.WriteLine(findSubstrings(S, k));    return 0;}} // This code is contributed by SoM15242.
<script>  // Javascript program to count number of substrings// in which each character has count less// than or equal to k. function findSubstrings(s, k){    // variable to store count of substrings.    var ans = 0;     // array to store count of each character.    var cnt = Array(26);     var i, j, n = s.length;    for (i = 0; i < n; i++) {        cnt = Array(26).fill(0);        for (j = i; j < n; j++) {             // increment character count            cnt[(s[j].charCodeAt(0) - 'a'.charCodeAt(0))]++;             // check only the count of current character            // because only the count of this            // character is changed. The ending point is            // incremented to current position only if            // all other characters have count at most            // k and hence their count is not checked.            // If count is less than k, then increase ans            // by 1.            if (cnt[(s[j].charCodeAt(0) - 'a'.charCodeAt(0))] <= k)                ans++;             // if count is less than k, then break as            // subsequent substrings for this starting            // point will also have count greater than            // k and hence are reduntant to check.            else                break;        }    }     // return the final count of substrings.    return ans;} // Driver codevar S = "aaabb";var k = 2;document.write( findSubstrings(S, k)); // This code is contributed by rrrtnx.</script>
Output:
 12
Time complexity: O(n^2) Auxiliary Space: O(1)Another efficient solution is to use sliding window technique. In which we will maintain two pointers left and right.We initialize left and the right pointer to 0, move the right pointer until the count of each alphabet is less than k, when the count is greater than we start incrementing left pointer and decrement the count of the corresponding alphabet, once the condition is satisfied we add (right-left + 1) to the answer. Implementation:  
C++
Java
Python
C#
Javascript
// CPP program to count number of substrings// in which each character has count less// than or equal to k.#include<bits/stdc++.h> using namespace std; //function to find number of substring//in which each character has count less// than or equal to k. int find_sub(string s,int k){    int len=s.length();    int lp=0,rp=0;             // initialize left and right pointer to 0    int ans=0;    int hash_char[26]={0};     // an array to keep track of count of each alphabet    for(;rp<len;rp++){        hash_char[s[rp]-'a']++;        while(hash_char[s[rp]-'a']>k){            hash_char[s[lp]-'a']--;   // decrement the count            lp++;         //increment left pointer        }        ans+=rp-lp+1;    }    return ans;} // Driver codeint main(){    string s="aaabb";    int k=2;    cout<<find_sub(s,k)<<endl;}
// Java program to count number of substrings// in which each character has count less// than or equal to k.class GFG{         //function to find number of substring    //in which each character has count less    // than or equal to k.    static int find_sub(String s, int k)    {        int len = s.length();         // initialize left and right pointer to 0        int lp = 0, rp = 0;        int ans = 0;         // an array to keep track of count of each alphabet        int[] hash_char = new int[26];        for (; rp < len; rp++)        {            hash_char[s.charAt(rp) - 'a']++;            while (hash_char[s.charAt(rp) - 'a'] > k)            {                // decrement the count                hash_char[s.charAt(lp) - 'a']--;                 //increment left pointer                lp++;            }            ans += rp - lp + 1;        }        return ans;    }     // Driver code    public static void main(String[] args)    {        String S = "aaabb";        int k = 2;        System.out.println(find_sub(S, k));    }} // This code is contributed by PrinciRaj1992
# Python3 program to count number of substrings# in which each character has count less# than or equal to k.  # function to find number of substring# in which each character has count less# than or equal to k. def find_sub(s, k):     Len = len(s)         # initialize left and right pointer to 0    lp, rp = 0, 0                     ans = 0     # an array to keep track of count of each alphabet    hash_char = [0 for i in range(256)]       for rp in range(Len):         hash_char[ord(s[rp])] += 1         while(hash_char[ord(s[rp])] > k):            hash_char[ord(s[lp])] -= 1 # decrement the count            lp += 1         #increment left pointer         ans += rp - lp + 1     return ans # Driver codes = "aaabb"k = 2;print(find_sub(s, k)) # This code is contributed by mohit kumar
// C# program to count number of substrings// in which each character has count less// than or equal to k.using System; class GFG{    //function to find number of substring    //in which each character has count less    // than or equal to k.    static int find_sub(string s, int k)    {        int len = s.Length;             // initialize left and right pointer to 0        int lp = 0,rp = 0;                    int ans = 0;             // an array to keep track of count of each alphabet        int []hash_char = new int[26];            for(;rp < len; rp++)        {            hash_char[s[rp] - 'a']++;            while(hash_char[s[rp] - 'a'] > k)            {                // decrement the count                hash_char[s[lp] - 'a']--;                              //increment left pointer                lp++;                   }            ans += rp - lp + 1;        }          return ans;     }     // Driver code    static public void Main()    {        String S = "aaabb";        int k = 2;        Console.WriteLine(find_sub(S, k));    }} // This code is contributed by Rajput-Ji
<script>// Javascript program to count number of substrings// in which each character has count less// than or equal to k.         //function to find number of substring    //in which each character has count less    // than or equal to k.    function find_sub(s,k)    {        let len = s.length;          // initialize left and right pointer to 0        let lp = 0, rp = 0;        let ans = 0;          // an array to keep track of count of each alphabet        let hash_char = new Array(26);        for(let i = 0; i < hash_char.length; i++)        {            hash_char[i] = 0;        }        for (; rp < len; rp++)        {            hash_char[s[rp].charCodeAt(0) - 'a'.charCodeAt(0)]++;            while (hash_char[s[rp].charCodeAt(0) - 'a'.charCodeAt(0)] > k)            {                             // decrement the count                hash_char[s[lp].charCodeAt(0) - 'a'.charCodeAt(0)]--;                  //increment left pointer                lp++;            }            ans += rp - lp + 1;        }        return ans;    }         // Driver code    let S = "aaabb";    let k = 2;    document.write(find_sub(S, k));                // This code is contributed by avanitrachhadiya2155</script>
Output:
 12
Time complexity: O(n) Auxiliary Space: O(1) 
YouTubeGeeksforGeeks507K subscribersCount substrings with each character occurring at most k times | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:07•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=cSa9ClP1Zo0" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 
shikharK
29AjayKumar
Rajput-Ji
SoumikMondal
ukasp
princiraj1992
mohit kumar 29
rrrtnx
avanitrachhadiya2155
frequency-counting
Strings
Technical Scripter
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not
KMP Algorithm for Pattern Searching
Array of Strings in C++ (5 Different Ways to Create)
Different methods to reverse a string in C/C++
Convert string to char array in C++
Longest Palindromic Substring | Set 1
Caesar Cipher in Cryptography
Check whether two strings are anagram of each other
Length of the longest substring without repeating characters