Longest Happy String - Problem
A string s is called happy if it satisfies the following conditions:
sonly contains the letters'a','b', and'c'.sdoes not contain any of"aaa","bbb", or"ccc"as a substring.scontains at mostaoccurrences of the letter'a'.scontains at mostboccurrences of the letter'b'.scontains at mostcoccurrences 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 "".
A substring is a contiguous sequence of characters within a string.
Input & Output
Example 1 — Basic Case
$
Input:
a = 1, b = 1, c = 7
›
Output:
"ccaccbcc"
💡 Note:
We have 7 c's, 1 a, and 1 b. Using a greedy approach: add cc, then a (to avoid 3 c's), then cc, then b, then cc. Result: ccaccbcc uses 6 c's, 1 a, 1 b = 8 characters total.
Example 2 — Equal Counts
$
Input:
a = 2, b = 2, c = 1
›
Output:
"aabbc"
💡 Note:
With equal counts of a and b, we can use both twice. Pattern: a-a-b-b-c uses all characters optimally.
Example 3 — No Solution
$
Input:
a = 0, b = 2, c = 1
›
Output:
"bbc"
💡 Note:
Only b and c available. We can create b-b-c without violating the three consecutive rule.
Constraints
- 0 ≤ a, b, c ≤ 100
- a + b + c > 0
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code