Longest substring of 0 s in a string formed by k concatenations | Function to calculate maximum length of substring containing only zero ; loop to calculate longest substring in string containing 0 's ; if all elements in string a are '0 ; Else , find prefix and suffix containing only zeroes ; Calculate length of the prefix containing only zeroes ; Calculate length of the suffix containing only zeroes ; if k <= 1 , no need to take suffix + prefix into account ; Driver program to test above function
def subzero ( str , k ) : NEW_LINE INDENT ans = 0 NEW_LINE curr = 0 NEW_LINE n = len ( str ) NEW_LINE for i in str : NEW_LINE INDENT if ( i == '0' ) : NEW_LINE INDENT curr += 1 NEW_LINE DEDENT else : NEW_LINE INDENT curr = 0 NEW_LINE DEDENT ans = max ( ans , curr ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( ans == n ) : NEW_LINE INDENT print ( n * k ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT pre = suff = 0 NEW_LINE for i in str : NEW_LINE INDENT if ( i == '0' ) : NEW_LINE INDENT pre += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( str [ i ] == '0' ) : NEW_LINE INDENT suff += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT if ( k > 1 ) : NEW_LINE INDENT ans = max ( ans , pre + suff ) NEW_LINE DEDENT print ( ans ) NEW_LINE return NEW_LINE DEDENT k = 5 NEW_LINE str = '00100110' NEW_LINE subzero ( str , k ) NEW_LINE