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 "".
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.
Constraints:
0 <= a, b, c <= 100a + b + c > 0class Solution {
public int[] sumZero(int n) {
int[] ans = new int[n];
for (int i = n % 2 + 1; i < n; i += 2) {
ans[i - 1] = i;
ans[i] = -i;
}
return ans;
}
}class Solution {
private record Node(int freq, char ch) {
}
public String longestDiverseString(int... args) {
Queue<Node> pq = new PriorityQueue<>((m, n) -> n.freq - m.freq);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < args.length; i++)
if (args[i] > 0)
pq.offer(new Node(args[i], (char) ('a' + i)));
Node top = pq.poll();
while (top != null) {
int f = top.freq, k = !pq.isEmpty() && pq.peek().freq >= f * 2 ? 1 : 2;
char ch = top.ch;
while (f > 0 && k-- > 0) {
sb.append(ch);
f--;
}
// System.out.println(pq);
if (pq.isEmpty())
break;
top = pq.poll();
if (f > 0)
pq.offer(new Node(f, ch));
}
return sb.toString();
}
}