当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误
- [C language] qsort function
- 對數據安全的思考(轉載)
- Idea new UI usage
- 在线问题与离线问题
- 模拟卷Leetcode【普通】1447. 最简分数
- Sqlmap tutorial (III) practical skills II
- E - 食物链
- Buuctf-[[gwctf 2019] I have a database (xiaoyute detailed explanation)
- MySQL之基础知识
猜你喜欢
【Postman】Collections配置运行过程
Buuctf-[gxyctf2019] no dolls (xiaoyute detailed explanation)
Sqlmap tutorial (III) practical skills II
假设检验学习笔记
Request forwarding and redirection
Database isolation level
Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
浅谈专项测试之弱网络测试
二维码的前世今生 与 六大测试点梳理
JWT-JSON WEB TOKEN
随机推荐
Buuctf-[gxyctf2019] no dolls (xiaoyute detailed explanation)
Redis 核心技术与实战之 基本架构:一个键值数据库包含什么?
Significance of unit testing
二维码的前世今生 与 六大测试点梳理
Isam2 and incrementalfixedlagsmooth instructions in gtsam
【eolink】PC客户端安装
GTSAM中李群的運用
[API interface tool] Introduction to postman interface
把el-tree选中的数组转换为数组对象
php使用redis实现分布式锁
单元测试的意义
[postman] the monitors monitoring API can run periodically
[postman] dynamic variable (also known as mock function)
ICLR 2022 spotlight | analog transformer: time series anomaly detection method based on correlation difference
【无App Push 通用测试方案
Amazon Engineer: eight important experiences I learned in my career
对数据安全的思考(转载)
Properties file
Hypothesis testing learning notes
isam2运行流程