Longest sub string of 0 's in a binary string which is repeated K times | Function to find the longest subof 0 's ; To store size of the string ; To store the required answer ; Find the longest subof 0 's ; Run a loop upto s [ i ] is zero ; Check the conditions ; Driver code ; Function call
def longest_substring ( s , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE if ( k > 1 ) : NEW_LINE INDENT s += s NEW_LINE n *= 2 NEW_LINE DEDENT ans = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT x = 0 NEW_LINE while ( i < n and s [ i ] == '0' ) : NEW_LINE INDENT x , i = x + 1 , i + 1 NEW_LINE DEDENT ans = max ( ans , x ) NEW_LINE i += 1 NEW_LINE DEDENT if ( k == 1 or ans != n ) : NEW_LINE INDENT return ans NEW_LINE DEDENT else : NEW_LINE INDENT return ( ans // 2 ) * k NEW_LINE DEDENT DEDENT s = "010001000" NEW_LINE k = 4 NEW_LINE print ( longest_substring ( s , k ) ) NEW_LINE