How to remove a pair of same adjacent characters from string in C#

1 Answer

0 votes
using System;

class Program
{
    static string removeAdjacentPair(string s) {
        for (int i = 0; i < s.Length - 1; i++) {
             if (s[i] == s[i + 1]) {
                 s = s.Remove(i, 2);
                 if (i != 0) i--;
             }
        }
        return s;
    }
    static void Main() {
        string s = "aabcccdeeffffgac";

        s = removeAdjacentPair(s);
        
        Console.Write(s);
    }
}



/*
run:

bcdgac

*/

 



answered Jun 20, 2020 by avibootz
...