力扣1405.最长快乐字符串
-
三个比较
- chlen记录最多剩余字母的数量
- xsize记录当前字符串
cpp
class Solution {
public:
string longestDiverseString(int a, int b, int c) {
string res;
while(a + b + c > 0)
{
char choose = '-';
int chlen = 0;
int xsize = res.size();
//a的数量更多 并且可以放
if(a > chlen && (xsize < 2 || res[xsize - 1] != 'a' || res[xsize - 2] != 'a'))
{
chlen = a;
choose = 'a';
}
//b的数量更多 并且可以放
if(b > chlen && (xsize < 2 || res[xsize - 1] != 'b' || res[xsize - 2] != 'b'))
{
chlen = b;
choose = 'b';
}
//c的数量更多 并且可以放
if(c > chlen && (xsize < 2 || res[xsize - 1] != 'c' || res[xsize - 2] != 'c'))
{
chlen = c;
choose = 'c';
}
if(choose == '-') break;
res.append(1,choose);
if(choose == 'a') a--;
else if(choose == 'b') b--;
else if(choose == 'c') c--;
}
return res;
}
};