Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.0 kB
3
Indexable
Never
// Online C++ compiler to run C++ program online
#include <iostream>
#include <vector>
using namespace std;

void countAll(string s, int idxEnd, char key, int &count, vector<int> &faster) {
    // cout << idxEnd << " - " << key << " - " << count << endl;
    // If we use flip 1
    int countSingleDiffLeft = 0;
    for (int j = 0; j < idxEnd; j++)
    {
        if (s[j] != key)
            countSingleDiffLeft++;
    }
    faster.push_back(count + countSingleDiffLeft);
    
    if (idxEnd == 0) {
        count += s[0] == key ? 0 : 1;
        return;
    }
    
    int i = idxEnd;
    while (s[i] == key) {
        i--; // Find first diff
    }
    
    if (i <= 0) {
        return;
    }
    count++;
    countAll(s, i, key == 'A' ? 'B' : 'A', count, faster);
}

int main() {
    // Write C++ code here
    string s = "AAABAAAAAAAAABAAABAABAABABBAAABABBABBABBBABAABABABBBABABABAAABAAAABBBBBABAAABABBBAAAABABBBAAABAAAABBABABBBBABABABBBBBABABABBBBBBAAABABABAABBABAAABBAABAAAAABABBABBBBABBBABABABABBABBBBBBBAAABAABBABBAABBBBBABBABABBBABABBABAAAAABABBBAABBABABBBABAAAAAAABBABBABABABABAAABBABABBABBABBAAAAAABABBABABAABBBBABBBBABABABBABABBBAABAAABBBBABABABABAAABBBABABABBABABABABBBAAAAAAABBBBBBABABAABBAAAABBBBBBAAABBBBAABBBABBBAABAAABBBAAAABBBABBBBAAABBBABBAABAABBAABAAAAABBBBBBBABABAABAABAABBABABABBBABBABBABBBABAABBBBBBAABAAAAAAAAABABAAABBABBAAAAAABAABABBAAABBBBBAAABABABBAAABBBBAAABABBBABAAABAAABBBBBBBBBBBABAAAAABAABABABABABBBAABAAAABABAABABBAAAABBABABBBABBABABBABBAAB";
    int count = 0;
    vector<int> changeIdx;
    vector<vector<int>> consecutive;
    int sLength = s.length();
    int changeArray[sLength] = {0};
    
    char lastChar = s[0];
    
    for (int i = 1; i < s.length(); i++)
    {
        if (lastChar != s[i])
        {
            changeIdx.push_back(i);
            changeArray[i] = 1;

            if (changeArray[i - 1]) {
                if (!consecutive.empty())
                {
                    if (consecutive.back().back() == i - 1)
                    {
                        consecutive.back().push_back(i);
                    }
                    else
                    {
                        consecutive.push_back({i - 1, i});
                    }
                }
                else
                {
                    consecutive.push_back({i - 1, i});
                }
            }

            lastChar = s[i];
        }
    }
    
    for (int i = 0; i < consecutive.size(); ++i)
    {
        int j;
        if (consecutive[i].size() % 2) {
            j = 1;
        } else {
            j = 0;
        }
        for (j; j < consecutive[i].size(); j += 2) {
            count++;
            s[consecutive[i][j]] = s[consecutive[i][j]] == 'A' ? 'B' : 'A';
        }
    }

    vector<int> g1;
    countAll(s, s.length() - 1, 'A', count, g1);
    g1.push_back(count);
    
    int min_count = count;
    
    for (auto i = g1.begin(); i != g1.end(); ++i)
        if (min_count > *i)
            min_count = *i;
    
    cout << min_count;

    return 0;
}
Leave a Comment