- Notifications
You must be signed in to change notification settings - Fork 625
/
Copy path984.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.
'''
classSolution(object):
defstrWithout3a3b(self, A, B):
"""
:type A: int
:type B: int
:rtype: str
"""
result=''
ifA>B:
whileB>0andA>0:
ifA-B>=3:
ifA>1:
result+='aab'
A-=2
else:
result+='ab'
A-=1
B-=1
else:
result+='ab'
A-=1
B-=1
ifA>0:
result+='a'*A
ifB>0:
result+='b'*B
else:
whileB>0andA>0:
ifB-A>=3:
ifB>1:
result+='bba'
B-=2
else:
result+='ba'
B-=1
A-=1
else:
result+='ba'
A-=1
B-=1
ifA>0:
result+='a'*A
ifB>0:
result+='b'*B
returnresult