# 1405最长快乐字符串
import java.util.Arrays;
/**
* @author yagol
* @date 下午12:57
* @desc the description of this class
**/
class Solution1405 {
public static void main(String[] args) {
System.out.println(new Solution1405().longestDiverseString(
1, 1, 7
));
}
public String longestDiverseString(int a, int b, int c) {
StringBuilder res = new StringBuilder();
Pair[] arr = {new Pair(a, 'a'), new Pair(b, 'b'), new Pair(c, 'c')};
while (true) {
Arrays.sort(arr, (p1, p2) -> p2.freq - p1.freq);
boolean hasNext = false;
for (Pair pair : arr) {
if (pair.freq <= 0) {
//如果从大往下有一个剩余的个数为0了,那剩下的肯定都是0了,跳出
break;
}
int m = res.length();
if (m >= 2 && res.charAt(m - 2) == pair.ch && res.charAt(m - 1) == pair.ch) {
continue;
}
hasNext = true;
res.append(pair.ch);
pair.freq--;
break;
}
if (!hasNext) {
break;
}
}
return res.toString();
}
class Pair {
int freq;
char ch;
public Pair(int freq, char ch) {
this.freq = freq;
this.ch = ch;
}
}
}
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54