-
Notifications
You must be signed in to change notification settings - Fork 8
/
string c++
35 lines (33 loc) · 1.05 KB
/
string c++
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
class Solution {
public:
std::string longestDiverseString(int a, int b, int c) {
int currA = 0, currB = 0, currC = 0;
int maxLen = a + b + c, i = 0;
std::string result; // Using std::string instead of StringBuilder
while (i < maxLen) {
if ((currA != 2 && a >= b && a >= c) || (a > 0 && (currB == 2 || currC == 2))) {
result += 'a';
currA++;
currB = 0;
currC = 0;
a--;
}
else if ((currB != 2 && b >= a && b >= c) || (b > 0 && (currA == 2 || currC == 2))) {
result += 'b';
currB++;
currA = 0;
currC = 0;
b--;
}
else if ((currC != 2 && c >= a && c >= b) || (c > 0 && (currA == 2 || currB == 2))) {
result += 'c';
currC++;
currA = 0;
currB = 0;
c--;
}
i++;
}
return result;
}
};