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.

&nbsp;
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.


&nbsp;
Constraints:


	0 &lt;= a, b, c &lt;= 100
	a + b + c &gt; 0


class Solution:
	def longestDiverseString(self, a: int, b: int, c: int) -> str:
		pq = []
		if a > 0: heapq.heappush(pq,(-a,'a'))        
		if b > 0: heapq.heappush(pq,(-b,'b'))        
		if c > 0: heapq.heappush(pq,(-c,'c'))

		ans = ''
		while pq:
			c, ch = heapq.heappop(pq)
			if len(ans)>1 and ans[-1] == ans[-2] == ch:
				if not pq: break
				c2, ch2 = heapq.heappop(pq)
				ans += ch2
				c2 += 1
				if c2: heapq.heappush(pq,(c2,ch2))
			else:
				ans += ch
				c += 1
			if c: heapq.heappush(pq,(c,ch))

		return ans
/*
The idea behid this problem
1. Here we start by taking the size as the sum of a, b, c.
2. Then we use 3 variables A, B, C to count the occurance of a, b, c.
3. Now we iterate until the size, and 
    -> Checks the largest number among a, b, c and whether the count < 2 or whther the count of other letters is 2 and there is still letters that can be added, then we append the letter, decrement from the total count of that particular letter and increase the occurance of that letter and set others back to zero.
    
4. Finally return the string.
*/
class Solution {
    public String longestDiverseString(int a, int b, int c) {
        int totalSize = a + b + c;
        int A = 0;
        int B = 0;
        int C = 0;
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<totalSize; i++) {
            // checks a is largest and its count still < 2 or B and C =2 and there are still a that can be added 
            if ((a>=b && a>=c && A<2) || (B==2 && a>0) || (C==2 && a>0)) {
                sb.append("a");
                a -= 1;
                A += 1;
                B = 0;
                C = 0;
            }
            // check b is largest and its count still < 2 or A and C = 2 and there are still b that cam be added
            else if ((b>=a && b>=c && B<2) || (A==2 && b>0) || (C==2 && b>0)) {
                sb.append("b");
                b -= 1;
                B += 1;
                A = 0;
                C = 0;
            }
            // checks c is largest and its count still < 2 or B and A = 2 and there are still c that can be added
            else if ((c>=a && c>=b && C<2) || (A==2 && c>0) || (B==2 && c>0)) {
                sb.append("c");
                c -= 1;
                C += 1;
                A = 0;
                B = 0;
            }
        }
        return sb.toString();
    }
}
class Solution {
public:
	#define f first    
	#define s second
	string longestDiverseString(int a, int b, int c) {
		priority_queue<pair<int,char>> pq;
		if(a>0)pq.push({a,'a'});        
		if(b>0)pq.push({b,'b'});        
		if(c>0)pq.push({c,'c'});

		string ans  = "";
		while(!pq.empty()){
			auto t = pq.top(); pq.pop();
			int c = t.f;
			char ch = t.s;
			if(ans.size() > 1 && ans[ans.size()-1]==ch && ch == ans[ans.size()-2]) {
				if(pq.empty())break;
				auto t = pq.top(); pq.pop();
				int c2 = t.f;
				char ch2 = t.s;
				ans += ch2;
				c2-=1;
				if(c2)pq.push({c2,ch2});
			}else{
				ans += ch;
				c-=1;
			}
			if(c)pq.push({c,ch});
		}
		return ans;
	}
};
var longestDiverseString = function(a, b, c) {
    let str = '', aCount = 0, bCount = 0, cCount = 0;
    let len = a + b + c;
    for(let i = 0; i < len; i++) {
        if (a >= b && a >= c && aCount != 2 || bCount == 2 && a > 0 || cCount == 2 && a > 0) {
            adjustCounts('a', aCount+1, 0, 0);
            a--;
        } else if (b >= a && b >= c && bCount != 2 || aCount == 2 && b > 0 || cCount == 2 && b > 0) {
            adjustCounts('b', 0, bCount+1, 0);
            b--;
        } else if (c >= a && c >= b && cCount != 2 || bCount == 2 && c > 0|| aCount == 2 && c > 0) {
            adjustCounts('c', 0, 0 , cCount+1);
            c--;
        }
    }

    function adjustCounts(letter, newA, newB, newC){
        aCount = newA;
        bCount = newB;
        cCount = newC;
        str += letter;
    }

    return str;
};
Longest Happy String