Beats 100.00% Users||C++ Code||Easy Understandable Solution
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind this approach is to iteratively construct a string by adding chara
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this approach is to iteratively construct a string by adding characters \'a\' and \'b\' while ensuring that no three consecutive characters are the same.\n\n# Approach\nThe approach involves using two variables a and b to keep track of the remaining counts of characters \'a\' and \'b\'. We start by constructing a temporary string s which holds the current constructed string res. We then check the last two characters of s to decide which character (\'a\' or \'b\') to append next. If the last two characters are \'aa\', we append \'b\' to res and decrement the count of b. If the last two characters are \'bb\', we append \'a\' to res and decrement the count of a. If the counts are not equal, we append the character with the larger count to res and decrement the corresponding count. This ensures that no three consecutive characters are the same. We repeat this process until both a and b become zero.\n\n# Complexity\n- Time complexity: O(max(a, b))\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    string strWithout3a3b(int a, int b) {\n        string res,s="";\n        while(a!=0|| b!=0){\n            s=res;\n            if(s.size()>=2 && s.substr(s.size()-2)=="aa"){\n                res+=\'b\';\n                b--;\n            }\n            else if(s.size()>=2 && s.substr(s.size()-2)=="bb"){\n                res+=\'a\';\n                a--;\n            }\n            else if(a>b){\n                res+=\'a\';\n                a--;\n            }\n            else{\n                res+=\'b\';\n                b--;\n            }\n\n        }\n        return res;\n        \n    }\n};\n```
['C++']