-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1405. Longest happy string
35 lines (32 loc) · 1.21 KB
/
1405. Longest happy string
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
//1405. Longest happy string
class Solution {
public String longestDiverseString(int a, int b, int c) {
StringBuilder sb = new StringBuilder();
int totalLength = a + b + c;
int continuousA = 0, continuousB = 0, continuousC = 0;
for(int i = 0; i < totalLength; i++) {
if((a >= b && a >= c && continuousA != 2) || (continuousB == 2 && a > 0) || (continuousC == 2 && a > 0)) {
sb.append("a");
a--;
continuousA++;
continuousB = 0;
continuousC = 0;
}
else if((b >= a && b >= c && continuousB != 2) || (continuousA == 2 && b > 0) || (continuousC == 2 && b > 0)) {
sb.append("b");
b--;
continuousB++;
continuousA = 0;
continuousC = 0;
}
else if((c >= a && c >= b && continuousC != 2) || (continuousB == 2 && c > 0) || (continuousA == 2 && c > 0)) {
sb.append("c");
c--;
continuousC++;
continuousA = 0;
continuousB = 0;
}
}
return sb.toString();
}
}