当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- LeetCode 729. 我的日程安排表 I
- LeetCode 1200. 最小绝对差
- Career advancement Guide: recommended books for people in big factories
- 模拟卷Leetcode【普通】1296. 划分数组为连续数字的集合
- Customize the gateway filter factory on the specified route
- 【eolink】PC客户端安装
- [API interface tool] Introduction to postman interface
- Seven imperceptible truths in software testing
- 职场进阶指南:大厂人必看书籍推荐
- Luogu p1460 [usaco2.1] healthy Holstein cows
猜你喜欢
随机推荐
多线程应用的测试与调试
Database - current read and snapshot read
PAT(乙级)2022年夏季考试
浅谈专项测试之弱网络测试
Request forwarding and redirection
Linux regularly backs up MySQL database
Accélération de la lecture vidéo de l'entreprise
[postman] the monitors monitoring API can run periodically
GTSAM中李群的运用
Company video accelerated playback
模拟卷Leetcode【普通】1447. 最简分数
模拟卷Leetcode【普通】1296. 划分数组为连续数字的集合
Redis 核心技术与实战之 基本架构:一个键值数据库包含什么?
GTSAM中ISAM2和IncrementalFixedLagSmoother说明
Construction and integration of Zipkin and sleuth for call chain monitoring
【Postman】测试(Tests)脚本编写和断言详解
测试周期被压缩?教你9个方法去应对
Manage configuration using Nacos
【Postman】Monitors 监测API可定时周期运行
Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai