-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSolution.txt
More file actions
46 lines (41 loc) · 1.23 KB
/
Solution.txt
File metadata and controls
46 lines (41 loc) · 1.23 KB
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
import java.io.*;
import java.util.*;
import java.lang.*;
public class solution
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String s = sc.next();
int n = s.length();
String ans = frequencySort(s);
System.out.println(ans);
}
public static String frequencySort(String s)
{
Map<Character,Integer> map =new HashMap<Character,Integer>();
for(int i=0;i<s.length();i++)
{
if(map.containsKey(s.charAt(i)))
map.put(s.charAt(i),map.get(s.charAt(i))+1);
else
map.put(s.charAt(i),1);
}
List<Map.Entry<Character,Integer>> sortedlist = new ArrayList<>(map.entrySet());
Collections.sort(sortedlist, new Comparator<Map.Entry<Character,Integer>>() {
@Override
public int compare(Map.Entry<Character, Integer> o1,
Map.Entry<Character, Integer> o2) {
return o2.getValue() - o1.getValue();
}
});
String lastString="";
for (Map.Entry<Character,Integer> e : sortedlist)
{
for(Integer j=0;j < e.getValue();j++)
lastString+= e.getKey().toString();
}
return lastString;
}
}