如果字符串中不含有任何 ‘aaa’,‘bbb’ 或 ‘ccc’ 這樣的字符串作為子串,那么該字符串就是一個「快樂字符串」。
給你三個整數 a,b ,c,請你返回 任意一個 滿足下列全部條件的字符串 s:
s 是一個盡可能長的快樂字符串。
s 中 最多 有a 個字母 ‘a’、b 個字母 ‘b’、c 個字母 ‘c’ 。
s 中只含有 ‘a’、‘b’ 、‘c’ 三種字母。
如果不存在這樣的字符串 s ,請返回一個空字符串 “”。
示例 1:
輸入:a = 1, b = 1, c = 7
輸出:“ccaccbcc”
解釋:“ccbccacc” 也是一種正確答案。
代碼
class Solution {public String longestDiverseString(int a, int b, int c) {int[] helper=new int[]{a,b,c};StringBuilder stringBuilder=new StringBuilder();int pre=-1,dou=-1;ArrayList<Character> list=new ArrayList<>();while (true){int max=-1,maxN=-1;for(int i=0;i<3;i++)//找出當前最多的字母{if(helper[i]==0||i==dou) continue;//已經選兩次或沒有了余量就不能選了if(helper[i]>=maxN){max=i;maxN=helper[i];}}if(max==-1) break;//找不到了就退出helper[max]--;if(pre==max)//和上一個字母相同,下次循環就不能再選了{dou=max;}else{dou=-1;}stringBuilder.append((char)(max+'a'));pre=max;}return stringBuilder.toString();}
}