-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBanner.java
More file actions
38 lines (38 loc) · 913 Bytes
/
Banner.java
File metadata and controls
38 lines (38 loc) · 913 Bytes
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
import java.util.*;
class Banner
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String s=in.next();
int f[]=new int[26];
for(char c:s.toCharArray())
f[c-'a']++;
PriorityQueue<int[]> pq=new PriorityQueue<>((x,y)->y[1]-x[1]);
for(int i=0;i<26;i++){
if(f[i]>0)
{
if(f[i]>(s.length()+1)/2)
{
System.out.println("");
return;
}
pq.add(new int[]{i,f[i]});
}
}
StringBuilder res=new StringBuilder();
while(pq.size()>1)
{
int a1[]=pq.poll();
int a2[]=pq.poll();
res.append((char)(a1[0]+'a')).append((char)(a2[0]+'a'));
if(--a1[1]>0)
pq.add(a1);
if(--a2[1]>0)
pq.add(a2);
}
if(!pq.isEmpty())
res.append((char)(pq.poll()[0]+'a'));
System.out.println(res.toString());
}
}