- 题目
- 代码
class Solution {
public String longestDiverseString(int a, int b, int c) {
List<Entry> list = new ArrayList<>();
list.add(new Entry('a', a));
list.add(new Entry('b', b));
list.add(new Entry('c', c));
Collections.sort(list, (e1, e2) -> e2.num - e1.num);
StringBuilder res = new StringBuilder();
while(list.get(0).num > 0){
if(res.length() < 2){
res.append(list.get(0).c);
list.get(0).num --;
}else if(res.charAt(res.length() - 1) == list.get(0).c &&
res.charAt(res.length() - 2) == list.get(0).c){
if(list.get(1).num == 0){
break;
}
res.append(list.get(1).c);
list.get(1).num --;
}else{
res.append(list.get(0).c);
list.get(0).num --;
}
Collections.sort(list, (e1, e2) -> e2.num - e1.num);
}
return res.toString();
}
}
class Entry{
char c;
int num;
public Entry(char c, int num){
this.c = c;
this.num = num;
}
}