当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- [postman] collections - run the imported data file of the configuration
- properties文件
- [API interface tool] Introduction to postman interface
- [postman] test script writing and assertion details
- Basic knowledge of error
- 假设检验学习笔记
- Manage configuration using Nacos
- G - Supermarket
- Left matching principle of joint index
- Leaflet map
猜你喜欢
LeetCode 731. 我的日程安排表 II
Application du Groupe Li dans gtsam
IP day 16 VLAN MPLS configuration
Postman核心功能解析-参数化和测试报告
[postman] test script writing and assertion details
Seven imperceptible truths in software testing
功能安全之故障(fault),错误(error),失效(failure)
ESP32 ESP-IDF看门狗TWDT
Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete
职场进阶指南:大厂人必看书籍推荐
随机推荐
Amazon Engineer: eight important experiences I learned in my career
模拟卷Leetcode【普通】1109. 航班预订统计
VINS-Mono: A Robust and Versatile Monocular Visual-Inertial State Estimator
Request forwarding and redirection
「 WEB测试工程师 」岗位一面总结
Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
MFC 动态创建的对话框及改变控件的大小和位置
Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
對數據安全的思考(轉載)
Leaflet map
模拟卷Leetcode【普通】1218. 最长定差子序列
Resttemplate and feign realize token transmission
联合索引的左匹配原则
LeetCode 729. 我的日程安排表 I
JWT-JSON WEB TOKEN
数学三大核心领域概述:几何
Win10 cannot operate (delete, cut) files
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
IP day 16 VLAN MPLS configuration
GTSAM中ISAM2和IncrementalFixedLagSmoother说明