-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1405.py
More file actions
41 lines (41 loc) · 1.31 KB
/
1405.py
File metadata and controls
41 lines (41 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution:
def longestDiverseString(self, a: int, b: int, c: int) -> str:
ans = ''
while a + b + c:
if a >= b and a >= c and not ans.endswith('aa'):
use = min(1 + int(not ans.endswith('a')), a)
ans += 'a' * use
a -= use
if b >= c and b:
ans += 'b'
b -= 1
elif c:
ans += 'c'
c -= 1
else:
return ans
elif b >= a and b >= c and not ans.endswith('bb'):
use = min(1 + int(not ans.endswith('b')), b)
ans += 'b' * use
b -= use
if a >= c and a:
ans += 'a'
a -= 1
elif c:
ans += 'c'
c -= 1
else:
return ans
elif not ans.endswith('cc'):
use = min(1 + int(not ans.endswith('c')), c)
ans += 'c' * use
c -= use
if b >= a and b:
ans += 'b'
b -= 1
elif a:
ans += 'a'
a -= 1
else:
return ans
return ans