Easy Approach and Easy to understand(Beat 100%) 😊😊
Approach and IntuitionThe problem requires constructing a string containing 'a' and 'b' such that no three consecutive 'a's or 'b's appear. Given two integers a
# Approach and Intuition
The problem requires constructing a string containing 'a' and 'b' such that no three consecutive 'a's or 'b's appear. Given two integers a and b, representing the count of 'a's and 'b's respectively, we need to construct the longest valid string.

# Intuition
* If a and b are nearly equal, we can alternate 'a' and 'b' ("ababab...").

* If one character count is significantly higher, we must distribute them carefully to avoid consecutive occurrences of three similar letters.

# Approach
1. Handle the case when a > b
* As long as a is much greater than b, add "aa" followed by "bb" if possible.
* When b is exhausted, append remaining 'a's one by one.

2. Handle the case when b > a

* Similarly, add "bb" followed by "aa" if possible.

* When a is exhausted, append remaining 'b's one by one.

3. Handle the case when a == b

* We can safely append alternating "ab" until both are exhausted.

# Time Complexity
* The while-loops iterate at most O(a + b) times, making the time complexity O(a + b).

# Space Complexity
* We store the output in a StringBuilder, resulting in O(a + b) space.

# Example Walkthrough
Example 1
🔹 Input: a = 4, b = 2
🔹 Processing:

* Append "aa" + "b" → "aab"

* Append "aa" + "b" → "aabaab"

* Append remaining "a" → "aabaaba" 🔹 Output: "aabaaba"

Example 2
🔹 Input: a = 3, b = 5
🔹 Processing:

* Append "bb" + "a" → "bba"

* Append "bb" + "a" → "bba bba"

* Append remaining "b" → "bba bba b" 🔹 Output: "bba bba b"
# Code
```java []
class Solution {
    public String strWithout3a3b(int a, int b) {
        StringBuilder ans = new StringBuilder();
        if(a > b){
            while(a > 0 && b > 0 && (a/2) < b){
                ans.append("aa").append("bb");
                b -= 2;
                a -= 2;
            }
            while(b > 0){
                ans.append("aa").append("b");
                b--;
                a -= 2;
            }
            while(a > 0){
                ans.append("a");
                a--;
            }
        }
        else if(b > a){
            while(b > 0 && a > 0 && (b/2) < a){
                ans.append("bb").append("aa");
                b -= 2;
                a -= 2;
            }
            while(a > 0){
                ans.append("bb").append("a");
                b -= 2;
                a--;
            }
            while(b > 0){
                ans.append("b");
                b--;
            }
        }
        else{
            while(b > 0 && a > 0){
                ans.append("b").append("a");
                b -= 1;
                a -= 1;
            }
        }
        return ans.toString();
    }
}
```
['String', 'Greedy', 'Java']