• + 0 comments

    Hint, there is no need to be efficient here, so just try what seems eassiest:

    int alternating(const string& str, char a, char b) {
        char prev = 0;
        int len = 0;
        for (char c : str) {
            if (c != a && c != b) continue; 
            if (c == prev) return -1;       
            prev = c;
            len++;
        }
        return len;
    }
    
    int alternate(string s) {
        set<char> str_set(s.begin(), s.end());
        vector<char> uniques;
        
        copy(str_set.begin(), str_set.end(), std::back_inserter(uniques));
        int bgs = 0;
        
        for(int i = 0; i<uniques.size(); i++){
            for (int j = i+1; j<uniques.size(); j++) {
                int len = alternating(s, uniques[i], uniques[j]);
                if(len>0 && len>bgs){
                    bgs = len;
                }
            } 
        }
        
        return bgs;
    }