blob: 738d00050ba46d3c8bc1aafb123b517dd764839a (
plain)
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
|
// 984. String Without AAA or BBB
#include "leetcode.h"
/*
* 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' and
* - the substring 'bbb' does not occur in 's'
*/
class Solution {
public:
string strWithout3a3b(int a, int b) {
string ans = "";
int cnta = 0, cntb = 0, total = a + b;
for (int i = 0; i < total; i++) {
if ((b >= a && cntb < 2) || (cnta == 2 && b)) {
ans += 'b';
b--;
cntb++;
cnta = 0;
} else if ((a >= b && cnta < 2) || (cntb == 2 && a)) {
ans += 'a';
a--;
cnta++;
cntb = 0;
}
}
return ans;
}
};
int main() {
Solution obj;
cout << obj.strWithout3a3b(1, 2) << endl; // expect: "abb"
cout << obj.strWithout3a3b(4, 1) << endl; // expect: "aabaa"
}
|