当前位置:网站首页>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 !
边栏推荐
- 自定义指定路由上的Gateway过滤器工厂
- MFC关于长字符串unsigned char与CString转换及显示问题
- P问题、NP问题、NPC问题、NP-hard问题详解
- Manhattan distance and Manhattan rectangle - print back font matrix
- 10M25DCF484C8G(FPGA) AMY-6M-0002 BGA GPS模块
- Function of activation function
- 还在为如何编写Web自动化测试用例而烦恼嘛?资深测试工程师手把手教你Selenium 测试用例编写
- leaflet 地图
- VINS-Mono: A Robust and Versatile Monocular Visual-Inertial State Estimator
- D - How Many Answers Are Wrong
猜你喜欢

P问题、NP问题、NPC问题、NP-hard问题详解

What are the test sites for tunnel engineering?
![Buuctf-[[gwctf 2019] I have a database (xiaoyute detailed explanation)](/img/2c/43ce298794589c5282edda94161d62.jpg)
Buuctf-[[gwctf 2019] I have a database (xiaoyute detailed explanation)

Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete

Pat (Grade B) 2022 summer exam

黑猫带你学UFS协议第4篇:UFS协议栈详解

Application of Lie group in gtsam
![[eolink] PC client installation](/img/91/8b3c4264e544b14f926e91edddf18d.png)
[eolink] PC client installation

【eolink】PC客户端安装

Significance of unit testing
随机推荐
G - Supermarket
Buuctf-[[gwctf 2019] I have a database (xiaoyute detailed explanation)
Leaflet map
模拟卷Leetcode【普通】1405. 最长快乐字符串
[wechat applet] build a development tool environment
Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)
selenium源码通读·9 |DesiredCapabilities类分析
Full link voltage measurement: building three models
Customize the gateway filter factory on the specified route
异常检测方法总结
LeetCode 729. 我的日程安排表 I
MySQL之基础知识
Overview of three core areas of Mathematics: geometry
职场进阶指南:大厂人必看书籍推荐
测试周期被压缩?教你9个方法去应对
Linux regularly backs up MySQL database
[C language] string left rotation
全链路压测:构建三大模型
自定义指定路由上的Gateway过滤器工厂
properties文件