-
Notifications
You must be signed in to change notification settings - Fork 5
/
StringWithoutAAAorBBB.java
63 lines (58 loc) · 1.44 KB
/
StringWithoutAAAorBBB.java
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
63
/*https://leetcode.com/problems/string-without-aaa-or-bbb/*/
class Solution {
public String strWithout3a3b(int A, int B) {
StringBuilder ans = new StringBuilder();
while (A > 0 || B > 0)
{
boolean writeA = false;
int L = ans.length();
if (L >= 2 && ans.charAt(L-1) == ans.charAt(L-2))
{
if (ans.charAt(L-1) == 'b')
writeA = true;
}
else
{
if (A >= B)
writeA = true;
}
if (writeA)
{
A--;
ans.append('a');
}
else
{
B--;
ans.append('b');
}
}
return ans.toString();
}
}
class Solution {
public String strWithout3a3b(int a, int b) {
StringBuilder sb = new StringBuilder();
int x = Math.min(a, Math.min(b, Math.abs(a - b)));
if (a > b){
sb.append("aab".repeat(x));
b -= x;
a -= 2 * x;
}
if (a < b){
sb.append("bba".repeat(x));
b -= 2 * x;
a -= x;
}
if (a == b){
sb.append("ab".repeat(a));
}
if (a == 0){
sb.append("b".repeat(b));
}
if (b == 0){
sb.append("a".repeat(a));
}
return sb.toString();
}
}