Longest sub string of 0 's in a binary string which is repeated K times | Function to find the longest substring of 0 's ; To store size of the string ; To store the required answer ; Find the longest substring of 0 's ; Run a loop upto s [ i ] is zero ; Check the conditions ; Driver code ; Function call
function longest_substring ( s , k ) { var n = s . length ; if ( k > 1 ) { s += s ; n *= 2 ; } var ans = 0 ; var i = 0 ; while ( i < n ) { var x = 0 ; while ( s [ i ] == ' ' && i < n ) x ++ , i ++ ; ans = Math . max ( ans , x ) ; i ++ ; } if ( k == 1 ans != n ) return ans ; else return ( ans / 2 ) * k ; } var s = " " ; var k = 4 ; document . write ( longest_substring ( s , k ) ) ;