Recursively remove all adjacent duplicates

1 min read

https://practice.geeksforgeeks.org/problems/consecutive-elements/0

class Solution {
public:
  string removeConsecutiveCharacter(string S) {
    int n = S.size();
    string ans = "";

    for (int i = 0; i < n - 1; i++) {
      ans += S[i];
      while (S[i] == S[i + 1]) {
        i++;
      }
    }

    if (S[n - 1] != S[n - 2])
      ans += S[n - 1];

    return ans;
  }
};
Find the smallest window in a string containing all characters of another string
String matching where one string contains wildcard characters