Skip to main content
LeetCode 1405, Medium. Topics: String, Greedy, Heap (Priority Queue). View on LeetCode. Generate this problem as a practice environment: tested reference solution, 18 parametrized pytest cases, and a playground notebook:

Problem

A string s is called happy if it satisfies the following conditions:
  • s only contains the letters 'a', 'b', and 'c'.
  • s does not contain any of "aaa", "bbb", or "ccc" as a substring.
  • s contains at most a occurrences of the letter 'a'.
  • s contains at most b occurrences of the letter 'b'.
  • s contains at most c occurrences of the letter 'c'.
Given three integers a, b, and c, return the longest possible happy string. If there are multiple longest happy strings, return any of them. If there is no such string, return the empty string "".

Examples

Constraints

  • 0 <= a, b, c <= 100
  • a + b + c > 0

Solution

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

Complexity

Tags

NeetCode 250, NeetCode All.
Last modified on August 25, 2026