Remove first adjacent pairs of similar characters until possible | Function to remove adjacent duplicates ; Iterate for every character in the string ; If ans string is empty or its last character does not match with the current character then append this character to the string ; Matches with the previous one ; Return the answer ; Driver Code
def removeDuplicates ( S ) : NEW_LINE INDENT ans = " " ; NEW_LINE for it in S : NEW_LINE INDENT if ( ans == " " or ans [ - 1 ] != it ) : NEW_LINE INDENT ans += it ; NEW_LINE DEDENT elif ( ans [ - 1 ] == it ) : NEW_LINE INDENT ans = ans [ : - 1 ] ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " keexxllx " ; NEW_LINE print ( removeDuplicates ( string ) ) ; NEW_LINE DEDENT