leetcode 1405 python3

"""如果字符串中不含有任何 '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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值