Alternating Characters

  • + 0 comments

    Here are my c++ approaches of solving this, video explanation here : https://youtu.be/KoDlxS38_ig

    Solution 1 : Using loop

    int alternatingCharacters(string s) {
        int result = 0;
        for(int i = 1; i < s.size(); i++) if(s[i] == s[i-1]) result++;
        return result;
    }
    

    Solution 2 : Using regex

    int alternatingCharacters(string s) {
        regex re("A{2,}|B{2,}");
        return s.size() - regex_replace(s, re, "*").size();
    }