# Easiest Bucket Sort

• Use a String array as the buckets. Whenever a new character comes, append that char to the string in that bucket

``````public class Solution {
public String frequencySort(String s) {
char[] sArr = s.toCharArray();
String[] buckets = new String[s.length()];
Map<Character,Integer> map = new HashMap<Character,Integer>();
for(char c: sArr){
map.put(c,!map.containsKey(c) ?1:map.get(c) + 1);
}
for(char c: map.keySet()){
int freq = map.get(c);
String str = new String();
if(buckets[freq]!= null)
str = buckets[freq];
str = str + c;
buckets[freq] = str;
}
StringBuffer buf = new StringBuffer();
for(int i = s.length() - 1 ;i>=0; i--){
if(buckets[i]!=null){
buf.append(deal(i,buckets[i]));
}
}
return buf.toString();
}
private StringBuffer deal(int freq, String str){
StringBuffer buf = new StringBuffer();
char[] list = str.toCharArray();
for(char c : list){
for(int i = 0 ;i< freq; i++){
buf.append(c);
}
}
return buf;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.