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
using System ; class GFG { static String removeDuplicates ( String S ) { String ans = " " ; for ( int i = 0 ; i < S . Length ; i ++ ) { if ( ans == " " ans [ ans . Length - 1 ] != S [ i ] ) ans += S [ i ] ; else if ( ans [ ans . Length - 1 ] == S [ i ] ) ans = ans . Substring ( 0 , ans . Length - 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { String str = " keexxllx " ; Console . WriteLine ( removeDuplicates ( str ) ) ; } }