/**
*
* @description 贪心算法: 每次循环都尽可能去消耗[当前出现]次数最多的字符
* @see https://leetcode-cn.com/problems/longest-happy-string/comments/329661
* @param {number} a
* @param {number} b
* @param {number} c
* @return {*} {string}
*/
function longestDiverseString(a: number, b: number, c: number): string {
let result: string = ``,
options: { char: string, count: number }[] = [{
char: 'a',
count: a,
}, {
char: 'b',
count: b,
}, {
char: 'c',
count: c,
}];
while (true) {
// 按照数量从大到小排序
options.sort((a, b) => b.count - a.count);
const { length } = result;
for (const option of options) {
const { char, count } = option;
if (!count) { // 次数用完了!
continue;
}
if (
result.length >= 2
&& char === result[result.length - 1]
&& char === result[result.length - 2]
) { // 再继续下去,就不能快乐的玩耍了
continue;
}
result += char;
--option.count;
break;
}
if (length === result.length) { // 字符串没有累加过,要么没得选(所有的count全为0),要么不能选(再选就要不快乐了),说明没有选择的余地了,只能退出!
break;
}
}
return result;
};