Skip to content

Commit 47d444e

Browse files
committed
✨ string without aaa or bbb
1 parent 6751089 commit 47d444e

File tree

1 file changed

+32
-0
lines changed
  • src/984-string-without-aaa-or-bbb

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number} A
3+
* @param {number} B
4+
* @return {string}
5+
*/
6+
var strWithout3a3b = function (A, B) {
7+
let res = '';
8+
const arr = [
9+
{ label: 'a', value: A },
10+
{ label: 'b', value: B },
11+
];
12+
13+
while (arr[0].value > 0 || arr[1].value > 0) {
14+
arr.sort((x, y) => x.value - y.value);
15+
const [min, max] = arr;
16+
17+
if (
18+
res.length >= 2 &&
19+
res[res.length - 1] === max.label &&
20+
res[res.length - 2] === max.label
21+
) {
22+
res += min.label;
23+
min.value--;
24+
continue;
25+
}
26+
27+
res += max.label;
28+
max.value--;
29+
}
30+
31+
return res;
32+
};

0 commit comments

Comments
 (0)