String Without AAA or BBB - Problem

Given two integers a and b, construct a string that contains exactly a occurrences of the character 'a' and exactly b occurrences of the character 'b'.

The challenge is to arrange these characters such that:

  • The total length of the string is a + b
  • No three consecutive 'a' characters appear together (no "aaa")
  • No three consecutive 'b' characters appear together (no "bbb")

Think of it as a balancing act - you need to distribute the characters strategically to avoid creating forbidden patterns while using all available characters.

For example, if a = 1 and b = 2, you could return "abb", "bab", or "bba" - all are valid solutions!

Input & Output

example_1.py โ€” Basic Case
$ Input: a = 1, b = 2
โ€บ Output: "abb"
๐Ÿ’ก Note: We have 1 'a' and 2 'b's to use. One valid arrangement is "abb" - it uses exactly 1 'a' and 2 'b's, has no 'aaa' or 'bbb' patterns. Other valid solutions include "bab" and "bba".
example_2.py โ€” Larger Difference
$ Input: a = 5, b = 1
โ€บ Output: "aabaaa"
๐Ÿ’ก Note: With 5 'a's and 1 'b', we need to distribute the 'a's carefully. The greedy approach gives "aabaaa" - it places 'b' strategically to break up the 'a's and prevent 'aaa' patterns while using all characters.
example_3.py โ€” Equal Counts
$ Input: a = 2, b = 2
โ€บ Output: "abab"
๐Ÿ’ก Note: When counts are equal, we can alternate characters safely. "abab" uses exactly 2 'a's and 2 'b's with no forbidden patterns. "baba" would also be valid.

Constraints

  • 0 โ‰ค a โ‰ค 100
  • 0 โ‰ค b โ‰ค 100
  • It is guaranteed that an answer exists for all valid inputs
  • At least one of a or b will be greater than 0

Visualization

Tap to expand
๐ŸŽต Musical Note Arrangement StrategyAvailable Notes:AAAAAB5 A-notes, 1 B-note๐ŸŽผ Composition Process:Step 1: A=5, B=1 โ†’ Play 'AA' (abundant note, 2 at once)AAStep 2: A=3, B=1 โ†’ Play 'B' (break pattern, avoid AAA)BStep 3: A=3, B=0 โ†’ Play 'AA' (safe to play 2 more)AAStep 4: A=1, B=0 โ†’ Play final 'A'A๐ŸŽต Final Melody: "AABAAA" ๐ŸŽตPerfect harmony - no jarring triple notes, all notes used!
Understanding the Visualization
1
Count Your Notes
Start with your available 'A' and 'B' notes, like having 5 A-notes and 1 B-note
2
Choose Wisely
Always prioritize the note you have more of - if you have many A's, play 2 A's together when safe
3
Break the Pattern
Add a different note to prevent monotony - place a B between groups of A's
4
Repeat Until Done
Continue this process until you've used all your notes, creating a balanced melody
Key Takeaway
๐ŸŽฏ Key Insight: Like composing music, we balance abundance with variety. Always use more of what you have plenty of, but break it up strategically to maintain harmony and avoid monotonous patterns!
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
73.9K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen