Check if given String can be split only into subsequences ABC

Check if given String can be split only into subsequences ABCGiven a string S of length N where each character of the string is either ‘A‘, ‘B‘ or ‘C‘. The task is to find if is it possible to split the string into subsequences “ABC“. If it is possible to split then print “Yes“. Otherwise, print “No“.Examples:Input: S = “ABABCC”Output: YesExplanation:One of the possible way of splitting is to split the string in 2 subsequences of “ABC” is following:First form a subsequence “ABC” by taking the character at indices 0, 1, and 4. Again form a subsequence “ABC” by taking the character at indices 2, 3, and 5. Therefore, the string can be split in 2 subsequences of “ABC”.Input: S = “AABBCC”Output: YesExplanation:One of the possible way of splitting is to split the string in 2 subsequences of “ABC” is following:First form a subsequence “ABC” by taking the character at indices 0, 2, and 4Again form a subsequence “ABC” by taking the character at indices 1, 3, and 5.Therefore, the string can be split in 2 subsequences of “ABC”.Input: S = “BAC”Output: NoApproach: The given problem can be solved based on the following observations: It can be observed that if N is not multiple of 3 or count of ‘A‘, ‘B‘, and ‘C‘ are not equal then it will be impossible to split the string satisfying the conditions.Also, for every ‘B‘ there must be at least one ‘A‘ to its left and one ‘C’ to its right.Follow the steps below to solve the problem:Initialize 3 deques of integers say A, B, and C to store the indices of characters ‘A‘, ‘B‘ and ‘C‘ respectively.Iterate over characters of the string S and in each iteration, if the current character is ‘A‘ then push the index i into A. Else if the current character is ‘B‘ then push the index i into B. Otherwise, push the index i into C.If N is not multiple of 3 and count of characters ‘A‘, ‘B‘ and ‘C‘ are not equal then print “No”.Otherwise, Iterate over the deque B using the variable i, and in each iteration, if B[i] is greater than the front element of deque A then Pop the front element of deque A. Otherwise print “No” and return.Now again iterate over the deque B using the variable i in reverse and in each iteration if B[i] is lesser than the last element of deque C then Pop the last element of the deque C. Otherwise print “No” and return.Finally, if none of the above cases satisfy then print “Yes” as the answer.Below is the implementation of the above approach:C++#include using namespace std;  string check(string S){            int N = S.length();          deque A;        deque B;        deque C;          for (int i = 0; i < N; i++) {                  if (S[i] == 'A') {                        A.push_back(i);        }                        else if (S[i] == 'B') {                        B.push_back(i);        }                          else {                        C.push_back(i);        }    }                  if (N % 3 || A.size() != B.size()        || A.size() != C.size()) {                  return "No";    }        for (int i = 0; i < B.size(); i++) {                                  if (!A.empty() && B[i] > A[0]) {                                    A.pop_front();        }                else {            return “No”;        }    }            for (int i = B.size() – 1; i >= 0; i–) {                                if (!C.empty() && B[i] < C.back()) {                                    C.pop_back();        }                else {            return "No";        }    }                  return "Yes";}  int main(){        string S = "ABABCC";          cout