Minimum number of removals required such that no subsequence of length 2 occurs more than once

Minimum number of removals required such that no subsequence of length 2 occurs more than onceGiven a string S consisting of N lowercase characters, the task is to modify the given string such that no subsequence of length two repeats in the string by removing minimum number of characters.Examples:Input: S = “abcaadbcd”Output: abcdExplanation: Removing the characters at indices {2, 3, 4, 5, 6, 7} modifies the string to “abcd”, that contains every subsequence of length 2 exactly once.Input: S = “cadbcc”Output: cadbcApproach: The given problem can be solved based on the observation that the final string can contain only unique characters with the exception that the first character can occur 2 times in the string, one at the start and the other at the end(if possible). Follow the steps below to solve the problem:Initialize an empty string, say ans to store the resultant final string.Initialize a boolean array C[] of size 26 to check if the character is present in the final string or not.Initialize a variable, say pos as 0 to store the index of the last character added to the string, ans.Traverse the given string S and if the current character is not present in ans, then append it to ans, mark it as visited in the array C[], and update the value of pos to i.Iterate over the range [pos + 1, N – 1] using the variable i and if S[i] is equal to S[0], then append it to the final string ans and break out of the loop.After completing the above steps, print the string ans as the result.Below is the implementation of the above approach:C++  #include using namespace std;  void RemoveCharacters(string s){        string ans = “”;              bool c[26];      for (int i = 0; i < 26; i++)        c[i] = 0;              int pos = 0;          for (int i = 0; i < s.size(); i++) {                          if (c[s[i] - 'a'] == 0) {            c[s[i] - 'a'] = 1;            pos = i;            ans += s[i];        }    }              for (int i = pos + 1;         i < (int)s.size(); i++) {                          if (s[i] == s[0]) {            ans += s[i];            break;        }    }          cout