Remove first adjacent pairs of similar characters until possible | C ++ implementation of the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string removeDuplicates ( string S ) { string ans = " " ; for ( auto it : S ) { if ( ans . empty ( ) or ans . back ( ) != it ) ans . push_back ( it ) ; else if ( ans . back ( ) == it ) ans . pop_back ( ) ; } return ans ; } int main ( ) { string str = " keexxllx " ; cout << removeDuplicates ( str ) ; }