#1405
Longest Happy String
specialist · 635 · lc medium +30 · verified · 65.5% accepted · 2,797 likes · top 70%
Description
A string s is happy if it satisfies all of the following:
- s only contains the letters 'a', 'b', and 'c'.
- s does not contain "aaa", "bbb", or "ccc" as a substring.
- s contains at most a occurrences of 'a'.
- s contains at most b occurrences of 'b'.
- s contains at most c occurrences of 'c'.
Given three integers a, b, and c, construct and return the longest possible happy string. Return any valid answer, or "" if none exists.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: a = 1, b = 1, c = 7
Output: "ccaccbcc"
Explanation: "ccbccacc" would also be a correct answer.
Example 2:
Input: a = 7, b = 1, c = 0
Output: "aabaa"
Explanation: It is the only correct answer in this case.
Code
1
2
3