Length of longest substring having all characters as K | Function to find the length of longest sub - string having all characters same as character K ; Initialize variables ; Iterate till size of string ; Check if current character is K ; Assingning the max value to max_len ; Driver code ; Function call
function length_substring ( S , K ) { let curr_cnt = 0 , prev_cnt = 0 , max_len ; for ( let i = 0 ; i < S . length ; i ++ ) { if ( S [ i ] == K ) { curr_cnt += 1 ; } else { prev_cnt = Math . max ( prev_cnt , curr_cnt ) ; curr_cnt = 0 ; } } prev_cnt = Math . max ( prev_cnt , curr_cnt ) ; max_len = prev_cnt ; return max_len ; } let S = " " ; let K = ' ' ; document . write ( length_substring ( S , K ) ) ;