Skip to main content

Longest Happy String

Problem Description​

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.

Examples​

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 <= 100
  • a + b + c > 0

Solution for Longest Happy String​

Approach​

this is almost identical to 984. String Without AAA or BBB. We just need to ignore the smallest count in each round. Aassuming a >= b >= c: always try to add 'aa'. If a - 2 >= b, add 'b' (if not, the next round will add 'bb'). Repeat recursivelly for the remaining counts.

In other words, we are greedily use two characters from the largest pile. We cusion these two characters with a character from the medium pile. For [11,5,3], as an example, we first generate 'aabaabaab', and our piles become [5,2,3]. At this time, c becomes the medium pile, and we generate '..aac' ([3,2,2]). After we add one more '..aa', c becomes the largest pile and we pull two characters from it '..cc' ``([1,2,0]). We add the rest '..bba', and the resulting string is 'aabaabaabaacaaccbba'.

This algorithm can be esilly proved to be correct by a contradiction (and counter-examples).

Code in Different Languages​

Written by @agarwalhimanshugaya
string longestDiverseString(int a, int b, int c, char aa = 'a', char bb = 'b', char cc = 'c') {
if (a < b)
return longestDiverseString(b, a, c, bb, aa, cc);
if (b < c)
return longestDiverseString(a, c, b, aa, cc, bb);
if (b == 0)
return string(min(2, a), aa);
auto use_a = min(2, a), use_b = a - use_a >= b ? 1 : 0;
return string(use_a, aa) + string(use_b, bb) +
longestDiverseString(a - use_a, b - use_b, c, aa, bb, cc);
}

Complexity Analysis​

Time Complexity: O(a+b+c)O(a+b+c)​

Space Complexity: O(a+b+c)O(a+b+c)​

References​