Reduce the string to minimum length with the given operation | Function to return the minimum possible length str can be reduced to with the given operation ; Stack to store the characters of the given string ; For every character of the string ; If the stack is empty then push the current character in the stack ; Get the top character ; If the top element is not equal to the current element and it only differs in the case ; Pop the top element from stack ; Else push the current element ; Driver code
def minLength ( string , l ) : NEW_LINE INDENT s = [ ] ; NEW_LINE for i in range ( l ) : NEW_LINE INDENT if ( len ( s ) == 0 ) : NEW_LINE INDENT s . append ( string [ i ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT c = s [ - 1 ] ; NEW_LINE if ( c != string [ i ] and c . upper ( ) == string [ i ] . upper ( ) ) : NEW_LINE INDENT s . pop ( ) ; NEW_LINE DEDENT else : NEW_LINE INDENT s . append ( string [ i ] ) ; NEW_LINE DEDENT DEDENT DEDENT return len ( s ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " ASbBsd " ; NEW_LINE l = len ( string ) ; NEW_LINE print ( minLength ( string , l ) ) ; NEW_LINE DEDENT