Longest happy string

class Solution:
    def longestDiverseString(self, a: int, b: int, c: int) -> str:
        res = []
        count = [
            [a, 'a'],
            [b, 'b'],
            [c, 'c']
        ]
        while True:
            count.sort(
                key=lambda x: -x[0]
            )
            flag = False
            for i in range(len(count)):
                num = count[i][0]
                char = count[i][1]
                if num <= 0:
                    continue
                if len(res) >= 2 and res[-1] == char and res[-2] == char:
                    continue
                flag = True
                res.append(char)
                count[i][0] -= 1
                break
            if not flag:
                break

        return "".join(res)

Longest Happy String

Difficulty: Medium


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