delete substrings/patterns from string

PHOTO EMBED

Fri Jul 01 2022 19:24:08 GMT+0000 (Coordinated Universal Time)

Saved by @jacobsfo

// you can use includes, for example:
 #include <algorithm>
#include <string>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;

string solution(string &S) {
    // write your code in C++14 (g++ 6.2.0)
    if(S.length()==1)
    {
       return to_string(S[0]);
    }
    
    int j=2;
    for(int i = 0; i < S.length();i++)
    {
        
       // cout << S.substr(i,j) << endl;
        if(S.substr(i,j)=="AB"|| S.substr(i,j)=="BA" || S.substr(i,j)=="CD" || S.substr(i,j)=="DC" )
    {
        S.erase(i,j);
        cout << S << endl;
        i=0;
        j=i+2;
    }
  
    }
    string one="CD";
    size_t pos = std::string::npos;
    while( (pos = S.find(one)) != std::string::npos )
    {
        S.erase(pos,one.length());
    }

    
    return S;

}
content_copyCOPY