Minimum removal of subsequences of distinct consecutive characters required to empty a given string | Function to count minimum operations required to make the string an empty string ; Stores count of 1 s by removing consecutive distinct subsequence ; Stores count of 0 s by removing consecutive distinct subsequence ; Traverse the string ; If current character is 0 ; Update cntOne ; Update cntZero ; If current character is 1 ; Update cntZero ; Update cntOne ; Driver code
def findMinOperationsReqEmpStr ( str ) : NEW_LINE INDENT cntOne = 0 NEW_LINE cntZero = 0 NEW_LINE for element in str : NEW_LINE INDENT if element == '0' : NEW_LINE INDENT if cntOne > 0 : NEW_LINE INDENT cntOne = cntOne - 1 NEW_LINE DEDENT cntZero = cntZero + 1 NEW_LINE DEDENT else : NEW_LINE INDENT if cntZero > 0 : NEW_LINE INDENT cntZero = cntZero - 1 NEW_LINE DEDENT cntOne = cntOne + 1 NEW_LINE DEDENT DEDENT return cntOne + cntZero NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = "0100100111" NEW_LINE print ( findMinOperationsReqEmpStr ( str ) ) NEW_LINE DEDENT