当前位置:网站首页>模拟卷Leetcode【普通】1405. 最长快乐字符串
模拟卷Leetcode【普通】1405. 最长快乐字符串
2022-07-06 06:15:00 【邂逅模拟卷】
汇总:模拟卷Leetcode 题解汇总
1405. 最长快乐字符串
如果字符串中不含有任何 ‘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
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-happy-string
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
class Solution:
def longestDiverseString(self, a: int, b: int, c: int) -> str:
cnt = {
'a':a,'b':b,'c':c}
res = ''
while True:
_,mid,most = sorted(cnt.keys(),key=lambda x:cnt[x])
if (len(res)<2 or not res[-2]==res[-1]==most) and cnt[most]:
res+=most
cnt[most]-=1
elif cnt[mid]:
res+=mid
cnt[mid]-=1
else:
break
return res
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list转node
return s.longestDiverseString(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- GTSAM中李群的運用
- Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误
- 一文揭开,测试外包公司的真 相
- G - Supermarket
- 数学三大核心领域概述:代数
- [wechat applet] build a development tool environment
- Function of contenttype
- 把el-tree选中的数组转换为数组对象
- What are the test sites for tunnel engineering?
- win10无法操作(删除、剪切)文件
猜你喜欢
随机推荐
数据库隔离级别
G - Supermarket
Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
单元测试的意义
Buuctf-[[gwctf 2019] I have a database (xiaoyute detailed explanation)
【Postman】Collections配置运行过程
GTSAM中李群的运用
[wechat applet] build a development tool environment
模拟卷Leetcode【普通】1314. 矩阵区域和
Accélération de la lecture vidéo de l'entreprise
D - How Many Answers Are Wrong
LeetCode 739. 每日温度
【Postman】动态变量(也称Mock函数)
「 WEB测试工程师 」岗位一面总结
Luogu p1460 [usaco2.1] healthy Holstein cows
Significance of unit testing
浅谈专项测试之弱网络测试
LeetCode 1200. 最小绝对差
模拟卷Leetcode【普通】1109. 航班预订统计
What are the test sites for tunnel engineering?