Simple StringBuilder Solution | 100% Beats
Intuition\n Describe your first thoughts on how to solve this problem. We want to construct a string with \'a\'s and \'b\'s such that there are no consecutive o
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->We want to construct a string with \'a\'s and \'b\'s such that there are no consecutive occurrences of more than three \'a\'s or \'b\'s.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n* We use a StringBuilder to construct the string.\n* While there are still \'a\'s or \'b\'s remaining, we add characters to the StringBuilder according to the conditions mentioned in the problem.\n# Complexity\n- Time complexity: O(a + b) since we iterate through both a and b.\n\n- Space complexity:O(a + b) for the StringBuilder.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public String strWithout3a3b(int a, int b) {\n       StringBuilder sb = new StringBuilder();\n       while(a > 0 || b > 0) {\n           if (a>b && b != 0) {\n               sb.append("aab");\n               a-=2;\n               b--;\n           } else if (b > a && a != 0) {\n               sb.append("bba");\n               b-=2;\n               a--;\n           } else {\n               if (a > 0) {\n                   sb.append("a");\n                   a--;\n               } else if(b > 0) {\n                   sb.append("b");\n                   b--;\n               }\n           }\n       }\n       return sb.toString();\n    }\n}\n```
['Greedy', 'Java']