1405. Longest Happy String
Problem
Build the longest possible string from at most a letters 'a', b letters 'b' and c letters 'c' such that it never contains "aaa", "bbb" or "ccc". Return any longest one (possibly "").
Example: a = 1, b = 1, c = 7 → "ccaccbcc" (or another valid string of length 8)
Greedy with a max-heap of remaining counts: append the most plentiful letter unless the last two characters are already that letter — then append the second most plentiful instead (and stop if there is none). Using the largest pile whenever allowed is what keeps the string as long as possible.
Complexity: O(a + b + c).
class Solution:
def longestDiverseString(self, a: int, b: int, c: int) -> str:
heap = [(-n, ch) for n, ch in ((a, "a"), (b, "b"), (c, "c")) if n]
heapify(heap)
out = []
while heap:
n1, c1 = heappop(heap)
if len(out) >= 2 and out[-1] == out[-2] == c1:
if not heap:
break
n2, c2 = heappop(heap)
out.append(c2)
if n2 + 1:
heappush(heap, (n2 + 1, c2))
heappush(heap, (n1, c1))
else:
out.append(c1)
if n1 + 1:
heappush(heap, (n1 + 1, c1))
return "".join(out)
def happy(s, a, b, c): # any valid answer of maximum length is accepted
x, y, z = sorted((a, b, c), reverse=True)
return ("aaa" not in s and "bbb" not in s and "ccc" not in s and s.count("a") <= a and s.count("b") <= b
and s.count("c") <= c and len(s) == y + z + min(x, 2 * (y + z + 1)))
for a, b, c in ((1, 1, 7), (7, 1, 0), (2, 2, 1), (0, 0, 0), (0, 8, 11), (5, 5, 5), (0, 0, 9)):
assert happy(Solution().longestDiverseString(a, b, c), a, b, c)
class Solution {
public String longestDiverseString(int a, int b, int c) {
PriorityQueue<int[]> pq = new PriorityQueue<>((x, y) -> y[0] - x[0]); // {count, letter}
if (a > 0) pq.add(new int[]{a, 'a'});
if (b > 0) pq.add(new int[]{b, 'b'});
if (c > 0) pq.add(new int[]{c, 'c'});
StringBuilder sb = new StringBuilder();
while (!pq.isEmpty()) {
int[] top = pq.poll();
int len = sb.length();
if (len >= 2 && sb.charAt(len - 1) == top[1] && sb.charAt(len - 2) == top[1]) {
if (pq.isEmpty()) break;
int[] second = pq.poll();
sb.append((char) second[1]);
if (--second[0] > 0) pq.add(second);
pq.add(top);
} else {
sb.append((char) top[1]);
if (--top[0] > 0) pq.add(top);
}
}
return sb.toString();
}
}
func longestDiverseString(a int, b int, c int) string {
cnt := []int{a, b, c}
out := []byte{}
for {
order := []int{0, 1, 2} // letters by remaining count, most first
sort.Slice(order, func(i, j int) bool { return cnt[order[i]] > cnt[order[j]] })
placed := false
for _, x := range order {
ch := byte('a' + x)
n := len(out)
if cnt[x] == 0 || (n >= 2 && out[n-1] == ch && out[n-2] == ch) {
continue // would make three in a row
}
out = append(out, ch)
cnt[x]--
placed = true
break
}
if !placed {
return string(out)
}
}
}