Given a string str and an integer K, the task is to find the length of the longest sub-string S’ such that every character in S’ appears at least K times.
 
Input: s = “xyxyyz”, k = 2
Output: 5
“xyxyy” is the longest sub-string where
every character appears at least twice.
Input: s = “neveropen”, k = 2
Output: 2
Approach: Consider all the possible sub-strings and for every sub-string, calculate the frequency of each of its character and check whether all the characters appear at least K times. For all such valid sub-strings, find the largest length possible.
Below is the implementation of the above approach: 
 
C++
| // C++ implementation of the approach#include <bits/stdc++.h>usingnamespacestd;#define MAX 26// Function that return true if frequency// of all the present characters is at least kboolatLeastK(intfreq[], intk){    for(inti = 0; i < MAX; i++) {        // If the character is present and        // its frequency is less than k        if(freq[i] != 0 && freq[i] < k)            returnfalse;    }    returntrue;}// Function to set every frequency to zerovoidsetZero(intfreq[]){    for(inti = 0; i < MAX; i++)        freq[i] = 0;}// Function to return the length of the longest// sub-string such that it contains every// character at least k timesintfindlength(string str, intn, intk){    // To store the required maximum length    intmaxLen = 0;    intfreq[MAX];    // Starting index of the sub-string    for(inti = 0; i < n; i++) {        setZero(freq);        // Ending index of the sub-string        for(intj = i; j < n; j++) {            freq[str[j] - 'a']++;            // If the frequency of every character            // of the current sub-string is at least k            if(atLeastK(freq, k)) {                // Update the maximum possible length                maxLen = max(maxLen, j - i + 1);            }        }    }    returnmaxLen;}// Driver codeintmain(){    string str = "xyxyyz";    intn = str.length();    intk = 2;    cout << findlength(str, n, k);    return0;} | 
Java
| // Java Implementation of the above approachclassGFG {staticfinalintMAX = 26;// Function that return true if frequency// of all the present characters is at least kstaticbooleanatLeastK(intfreq[], intk){    for(inti = 0; i < MAX; i++)     {        // If the character is present and        // its frequency is less than k        if(freq[i] != 0&& freq[i] < k)            returnfalse;    }    returntrue;}// Function to set every frequency to zerostaticvoidsetZero(intfreq[]){    for(inti = 0; i < MAX; i++)        freq[i] = 0;}// Function to return the length of the longest// sub-string such that it contains every// character at least k timesstaticintfindlength(String str, intn, intk){    // To store the required maximum length    intmaxLen = 0;    intfreq[] = newint[MAX];    // Starting index of the sub-string    for(inti = 0; i < n; i++)     {        setZero(freq);        // Ending index of the sub-string        for(intj = i; j < n; j++)        {            freq[str.charAt(j) - 'a']++;            // If the frequency of every character            // of the current sub-string is at least k            if(atLeastK(freq, k))            {                // Update the maximum possible length                maxLen = Math.max(maxLen, j - i + 1);            }        }    }    returnmaxLen;}// Driver codepublicstaticvoidmain(String args[]) {    String str = "xyxyyz";    intn = str.length();    intk = 2;    System.out.println(findlength(str, n, k));}}// This code has been contributed by 29AjayKumar | 
Python3
| # Python3 implementation of the approach MAX=26# Function that return true if frequency # of all the present characters is at least k defatLeastK(freq, k) :     fori inrange(MAX) :        # If the character is present and         # its frequency is less than k         if(freq[i] !=0andfreq[i] < k) :            returnFalse;         returnTrue; # Function to set every frequency to zero defsetZero(freq) :     fori inrange(MAX) :        freq[i] =0; # Function to return the length of the longest # sub-string such that it contains every # character at least k times deffindlength(string, n, k) :     # To store the required maximum length     maxLen =0;     freq =[0]*MAX;     # Starting index of the sub-string     fori inrange(n) :        setZero(freq);         # Ending index of the sub-string         forj inrange(i,n) :            freq[ord(string[j]) -ord('a')] +=1;             # If the frequency of every character             # of the current sub-string is at least k             if(atLeastK(freq, k)) :                # Update the maximum possible length                 maxLen =max(maxLen, j -i +1);             returnmaxLen; # Driver code if__name__ =="__main__":     string ="xyxyyz";     n =len(string);     k =2;     print(findlength(string, n, k));     # This code is contributed by AnkitRai01 | 
C#
| // C# Implementation of the above approachusingSystem;    classGFG {staticintMAX = 26;// Function that return true if frequency// of all the present characters is at least kstaticBoolean atLeastK(int[]freq, intk){    for(inti = 0; i < MAX; i++)     {        // If the character is present and        // its frequency is less than k        if(freq[i] != 0 && freq[i] < k)            returnfalse;    }    returntrue;}// Function to set every frequency to zerostaticvoidsetZero(int[]freq){    for(inti = 0; i < MAX; i++)        freq[i] = 0;}// Function to return the length of the longest// sub-string such that it contains every// character at least k timesstaticintfindlength(String str, intn, intk){    // To store the required maximum length    intmaxLen = 0;    int[]freq = newint[MAX];    // Starting index of the sub-string    for(inti = 0; i < n; i++)     {        setZero(freq);        // Ending index of the sub-string        for(intj = i; j < n; j++)        {            freq[str[j] - 'a']++;            // If the frequency of every character            // of the current sub-string is at least k            if(atLeastK(freq, k))            {                // Update the maximum possible length                maxLen = Math.Max(maxLen, j - i + 1);            }        }    }    returnmaxLen;}// Driver codepublicstaticvoidMain(String []args) {    String str = "xyxyyz";    intn = str.Length;    intk = 2;    Console.WriteLine(findlength(str, n, k));}}// This code is contributed by Princi Singh | 
Javascript
| <script>// Javascript implementation of the approachvarMAX = 26;// Function that return true if frequency// of all the present characters is at least kfunctionatLeastK(freq, k){    for(vari = 0; i < MAX; i++) {        // If the character is present and        // its frequency is less than k        if(freq[i] != 0 && freq[i] < k)            returnfalse;    }    returntrue;}// Function to set every frequency to zerofunctionsetZero(freq){    for(vari = 0; i < MAX; i++)        freq[i] = 0;}// Function to return the length of the longest// sub-string such that it contains every// character at least k timesfunctionfindlength(str, n, k){    // To store the required maximum length    varmaxLen = 0;    varfreq = Array(MAX).fill(0);    // Starting index of the sub-string    for(vari = 0; i < n; i++) {        setZero(freq);        // Ending index of the sub-string        for(varj = i; j < n; j++) {            freq[str[j].charCodeAt(0) - 'a'.charCodeAt(0)]++;            // If the frequency of every character            // of the current sub-string is at least k            if(atLeastK(freq, k)) {                // Update the maximum possible length                maxLen = Math.max(maxLen, j - i + 1);            }        }    }    returnmaxLen;}// Driver codevarstr = "xyxyyz";varn = str.length;vark = 2;document.write( findlength(str, n, k));</script> | 
5
Time Complexity: O(n2)
Auxiliary Space: O(MAX), where MAX is the maximum number of unique characters in the string.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

