Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fbeaa35

Browse files
author
Dream
committed
[update] 984
1 parent a6a1a85 commit fbeaa35

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
|987|[Vertical Order Traversal of a Binary Tree](https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/) | |Medium|
2626
|986|[Interval List Intersections](https://leetcode.com/problems/interval-list-intersections/) | |Medium|
2727
|985|[Sum of Even Numbers After Queries](https://leetcode.com/problems/sum-of-even-numbers-after-queries/) | |Easy|
28-
|984|[String Without AAA or BBB](https://leetcode.com/problems/string-without-aaa-or-bbb/) | |Easy|
28+
|984|[String Without AAA or BBB](https://leetcode.com/problems/string-without-aaa-or-bbb/) | [java_dream](./algorithms/stringWithoutAaaOrBbb/StringWithoutAaaOrBbb.java) |Easy|
2929
|983|[Minimum Cost For Tickets](https://leetcode.com/problems/minimum-cost-for-tickets/) | |Medium|
3030
|982|[Triples with Bitwise AND Equal To Zero](https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/) | |Hard|
3131
|981|[Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/) | |Medium|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @author Dream
3+
*/
4+
public class StringWithoutAaaOrBbb {
5+
public String strWithout3a3b(int a, int b) {
6+
StringBuffer buffer = new StringBuffer();
7+
int subtract = Math.abs(a - b);
8+
if (subtract >= 3) {
9+
if (a > b) {
10+
while (a > 1 && b > 0) {
11+
buffer.append("aab");
12+
a -= 2;
13+
b--;
14+
}
15+
}else {
16+
while (a > 0 && b > 1) {
17+
buffer.append("bba");
18+
b -= 2;
19+
a--;
20+
}
21+
}
22+
}
23+
while (a > 0 && b > 0) {
24+
if (a >= b) {
25+
buffer.append("ab");
26+
}
27+
if (b > a) {
28+
buffer.append("ba");
29+
}
30+
a--;
31+
b--;
32+
}
33+
while (a > 0) {
34+
buffer.append("a");
35+
a--;
36+
}
37+
while (b > 0) {
38+
buffer.append("b");
39+
b--;
40+
}
41+
return buffer.toString();
42+
}
43+
}

0 commit comments

Comments
 (0)