Longest substring of 0 s in a string formed by k concatenations | Function to calculate maximum length of substring containing only zero ; loop to first calculate longest substring in string ; if all elements in string are '0' ; Else , find size of prefix and suffix containing only zeroes ; Calculate prefix containing only zeroes ; Calculate suffix containing only zeroes ; if k <= 1 then there is no need to take prefix + suffix into account ; Drivers code
function subzero ( s , k ) { let ans = 0 , curr = 0 ; let len = s . length ; let str = s . split ( " " ) ; for ( let i = 0 ; i < len ; ++ i ) { if ( str [ i ] == ' ' ) curr ++ ; else curr = 0 ; ans = Math . max ( ans , curr ) ; } if ( ans == len ) return len * k ; else { let pre = 0 , suff = 0 ; for ( let i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' ' ) pre ++ ; else break ; } for ( let i = len - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' ' ) suff ++ ; else break ; } if ( k > 1 ) ans = Math . max ( ans , pre + suff ) ; return ans ; } } let str = " " ; let k = 5 ; document . write ( subzero ( str , k ) ) ;