Identity number which appears exactly once.

Similar Problems:

A string is called happy if it does not have any of the strings ‘aaa’, ‘bbb’ or ‘ccc’ as a substring.

Given three integers a, b and c, return any string s, which satisfies following conditions:

- s is happy and longest possible.
- s contains at most a occurrences of the letter ‘a’, at most b occurrences of the letter ‘b’ and at most c occurrences of the letter ‘c’.
- s will only contain ‘a’, ‘b’ and ‘c’ letters.

If there is no such string s return the empty string “”.

Example 1:

Input: a = 1, b = 1, c = 7 Output: "ccaccbcc" Explanation: "ccbccacc" would also be a correct answer.

Example 2:

Input: a = 2, b = 2, c = 1 Output: "aabbc"

Example 3:

Input: a = 7, b = 1, c = 0 Output: "aabaa" Explanation: It's the only correct answer in this case.

Constraints:

- 0 <= a, b, c <= 100
- a + b + c > 0

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.

- Solution:

## https://code.dennyzhang.com/longest-happy-string ## Basic Ideas: greedy ## ## Complexity: Time (n), Space O(n) class Solution: def longestDiverseString(self, a: int, b: int, c: int) -> str: l = [] # previous cnt of corresponding character A, B, C = 0, 0, 0 for i in range(a+b+c): if (a == max(a, b, c) and A != 2) or (B == 2 and a>0) or (C == 2 and a>0): # Case 1: a is the one with largest occcurent, append a if possible # Case 2: The previous ends with "BB". We can safely append one a. But why? l.append("a") A, a = A+1, a-1 B, C = 0, 0 elif (b == max(a, b, c) and B != 2) or (A == 2 and b>0) or (C==2 and b>0): l.append("b") B, b = B+1, b-1 A, C = 0, 0 elif (c == max(a, b, c) and C != 2) or (B == 2 and c>0) or (A==2 and c>0): l.append("c") C, c = C+1, c-1 A, B = 0, 0 return "".join(l)