Maximum count of sub | Function to return the count of the required sub - strings ; Iterate over all characters ; Count with current character ; If the substring has a length k then increment count with current character ; Update max count ; Driver Code
function maxSubStrings ( s , k ) { var maxSubStr = 0 , n = s . length ; for ( var c = 0 ; c < 26 ; c ++ ) { var ch = String . fromCharCode ( " " . charCodeAt ( 0 ) + c ) ; var curr = 0 ; for ( var i = 0 ; i <= n - k ; i ++ ) { if ( s [ i ] !== ch ) continue ; var cnt = 0 ; while ( i < n && s [ i ] === ch && cnt !== k ) { i ++ ; cnt ++ ; } i -- ; if ( cnt === k ) curr ++ ; } maxSubStr = Math . max ( maxSubStr , curr ) ; } return maxSubStr ; } var s = " " ; var k = 2 ; document . write ( maxSubStrings ( s , k ) ) ;