Largest sub | Java Implementation of the above approach ; Function that return true if frequency of all the present characters is at least k ; If the character is present and its frequency is less than k ; Function to set every frequency to zero ; Function to return the length of the longest sub - string such that it contains every character at least k times ; To store the required maximum length ; Starting index of the sub - string ; Ending index of the sub - string ; If the frequency of every character of the current sub - string is at least k ; Update the maximum possible length ; Driver code
class GFG { static final int MAX = 26 ; static boolean atLeastK ( int freq [ ] , int k ) { for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] != 0 && freq [ i ] < k ) return false ; } return true ; } static void setZero ( int freq [ ] ) { for ( int i = 0 ; i < MAX ; i ++ ) freq [ i ] = 0 ; } static int findlength ( String str , int n , int k ) { int maxLen = 0 ; int freq [ ] = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { setZero ( freq ) ; for ( int j = i ; j < n ; j ++ ) { freq [ str . charAt ( j ) - ' a ' ] ++ ; if ( atLeastK ( freq , k ) ) { maxLen = Math . max ( maxLen , j - i + 1 ) ; } } } return maxLen ; } public static void main ( String args [ ] ) { String str = " xyxyyz " ; int n = str . length ( ) ; int k = 2 ; System . out . println ( findlength ( str , n , k ) ) ; } }