当前位置:网站首页>Simulation volume leetcode [general] 1405 Longest happy string
Simulation volume leetcode [general] 1405 Longest happy string
2022-07-06 06:18:00 【Encounter simulation volume】
Summary : Simulation volume Leetcode Summary of questions
1405. The longest happy string
If the string does not contain any ‘aaa’,‘bbb’ or ‘ccc’ Such a string is used as a substring , Then the string is a 「 Happy string 」.
Here are three integers a,b ,c, Please return Any one A string that satisfies all of the following conditions s:
s Is a happy string as long as possible .
s in most Yes a Letters ‘a’、b Letters ‘b’、c Letters ‘c’ .
s It only contains ‘a’、‘b’ 、‘c’ Three letters .
If such a string does not exist s , Please return an empty string “”.
Example 1:
Input :a = 1, b = 1, c = 7
Output :“ccaccbcc”
explain :“ccbccacc” It is also a correct answer .
Example 2:
Input :a = 2, b = 2, c = 1
Output :“aabbc”
Example 3:
Input :a = 7, b = 1, c = 0
Output :“aabaa”
explain : This is the only correct answer to the test case .
Tips :
0 <= a, b, c <= 100
a + b + c > 0
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/longest-happy-string
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
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 turn 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')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
- MFC 动态创建的对话框及改变控件的大小和位置
- 模拟卷Leetcode【普通】1218. 最长定差子序列
- 在uni-app中使用腾讯视频插件播放视频
- Technology sharing | common interface protocol analysis
- E - food chain
- Luogu p1460 [usaco2.1] healthy Holstein cows
- Reading notes of effective managers
- Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)
- Eigen稀疏矩阵操作
- 10m25dcf484c8g (FPGA) amy-6m-0002 BGA GPS module
猜你喜欢

Redis 核心技术与实战之 基本架构:一个键值数据库包含什么?

MySQL之基础知识

LeetCode 1200. 最小绝对差

What are the test sites for tunnel engineering?

G - Supermarket

Win10 cannot operate (delete, cut) files
![[postman] collections - run the imported data file of the configuration](/img/85/7ac9976fb09c465c88f376b2446517.png)
[postman] collections - run the imported data file of the configuration
![[wechat applet] build a development tool environment](/img/f6/51f97b1c927337b34c5b3a4207abb4.png)
[wechat applet] build a development tool environment

【API接口工具】postman-界面使用介绍

数据库-当前读与快照读
随机推荐
[postman] dynamic variable (also known as mock function)
Database isolation level
MySQL之数据类型
On weak network test of special test
Nodejs realizes the third-party login of Weibo
D - How Many Answers Are Wrong
Manhattan distance sum - print diamond
Buuctf-[[gwctf 2019] I have a database (xiaoyute detailed explanation)
浅谈专项测试之弱网络测试
10m25dcf484c8g (FPGA) amy-6m-0002 BGA GPS module
Eigen稀疏矩阵操作
Is the test cycle compressed? Teach you 9 ways to deal with it
二维码的前世今生 与 六大测试点梳理
Overview of three core areas of Mathematics: geometry
一文揭开,测试外包公司的真 相
[eolink] PC client installation
Application du Groupe Li dans gtsam
Basic knowledge of error
曼哈顿距离和-打印菱形
Understanding of processes and threads