Largest sub | Javascript implementation of the 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
var MAX = 26 ; function atLeastK ( freq , k ) { for ( var i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] != 0 && freq [ i ] < k ) return false ; } return true ; } function setZero ( freq ) { for ( var i = 0 ; i < MAX ; i ++ ) freq [ i ] = 0 ; } function findlength ( str , n , k ) { var maxLen = 0 ; var freq = Array ( MAX ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { setZero ( freq ) ; for ( var j = i ; j < n ; j ++ ) { freq [ str [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; if ( atLeastK ( freq , k ) ) { maxLen = Math . max ( maxLen , j - i + 1 ) ; } } } return maxLen ; } var str = " " ; var n = str . length ; var k = 2 ; document . write ( findlength ( str , n , k ) ) ;