-
Notifications
You must be signed in to change notification settings - Fork 613
/
984.py
62 lines (55 loc) · 1.57 KB
/
984.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''
Given two integers A and B, return any string S such that:
S has length A + B and contains exactly A 'a' letters, and exactly B 'b' letters;
The substring 'aaa' does not occur in S;
The substring 'bbb' does not occur in S.
Example 1:
Input: A = 1, B = 2
Output: "abb"
Explanation: "abb", "bab" and "bba" are all correct answers.
'''
class Solution(object):
def strWithout3a3b(self, A, B):
"""
:type A: int
:type B: int
:rtype: str
"""
result = ''
if A > B:
while B > 0 and A > 0:
if A-B >= 3:
if A > 1:
result += 'aab'
A -= 2
else:
result += 'ab'
A -= 1
B -= 1
else:
result += 'ab'
A -= 1
B -= 1
if A > 0:
result += 'a'*A
if B > 0:
result += 'b'*B
else:
while B > 0 and A > 0:
if B-A >= 3:
if B > 1:
result += 'bba'
B -= 2
else:
result += 'ba'
B -= 1
A -= 1
else:
result += 'ba'
A -= 1
B -= 1
if A > 0:
result += 'a'*A
if B > 0:
result += 'b'*B
return result