Skip to main content
LeetCode 984, Medium. Topics: String, Greedy. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 16 parametrized pytest cases, and a playground notebook:

Problem

Given two integers a and b, return any string s such that:
  • s has length a + b and contains exactly a 'a' letters, and exactly b 'b' letters,
  • The substring 'aaa' does not occur in s, and
  • The substring 'bbb' does not occur in s.

Examples

Constraints

  • 0 <= a, b <= 100
  • It is guaranteed such an s exists for the given a and b.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026