"""如果字符串中不含有任何 '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" 也是一種正確答案。
示例 2:
輸入:a = 2, b = 2, c = 1
輸出:"aabbc"
示例 3:
輸入:a = 7, b = 1, c = 0
輸出:"aabaa"
解釋:這是該測試用例的唯一正確答案。
提示:
0 <= a, b, c <= 100
a + b + c > 0
"""
import copy
class Solution:
def is_not_empty(self,dic):
flag = True
for i in dic:
if dic[i]!=0:
flag=False
return flag
def get_max(self,dic):
max_flag = max([dic[i] for i in dic])
for i in dic:
if dic[i] == max_flag:
return i
else:
return None
def return_max(self,last,dic):
max_value = self.get_max(dic)
print("max_value:",max_value)
return_dic = copy.deepcopy(dic)
if last!=max_value:
if dic[max_value]<2:
s = "{}".format(max_value)
return_dic[max_value]-=1
else:
s = "{}".format(max_value*2)
return_dic[max_value]-=2
else:
del dic[max_value]
max_value = self.get_max(dic)
s = "{}".format(max_value)
return_dic[max_value]-=1
return s,return_dic,max_value
def longestDiverseString(self,a,b,c):
max_flag = max(a,b,c)
dic = {"a":a,"b":b,"c":c}
max_value="a"
for i in dic:
if dic[i]==max_flag:
max_value=i
sum_number = sum([dic[i] for i in dic if i!=max_value])
change_max = max_flag if sum_number*2+2>max_flag else sum_number*2+2
dic[max_value]=change_max
max_value = ''
res = ''
while not self.is_not_empty(dic):
s,dic,max_value = self.return_max(max_value,dic)
res+=s
return res
test = Solution()
test.longestDiverseString(1,1,7)