Easy to understand Java Solution using TreeMap


  • 0
    E
    public class Solution {
        public String frequencySort(String s) {
          TreeSet<Letter> treeset = new TreeSet<>();
          HashSet<Character> set = new HashSet<>();
          HashMap<Character,Integer> map = getCounts(s);
          StringBuilder result = new StringBuilder();
          
          for(int i = 0; i < s.length(); i++) 
            set.add(s.charAt(i));
            
          for(Character c : set)
            treeset.add(new Letter(c, map.get(c)));
             
          for(Letter letter : treeset)
            for(int j = 0; j < letter.count; j++)
              result.append(letter.id);
          
          return result.toString();
        }
        
        public static HashMap<Character,Integer> getCounts(String s){
          HashMap<Character, Integer> map = new HashMap<>();
          for(int i = 0; i < s.length(); i++)
            map.put(s.charAt(i), map.getOrDefault(s.charAt(i),0) + 1);
            
          return map;    
        }
    }
    
     class Letter implements Comparable<Letter>{
        char id;
        int  count;
        
        public Letter(char id, int count){
            this.id = id;
            this.count = count;
        }
        
        public int compareTo(Letter other){
         if(this.count < other.count) return 1;
        return -1;
        }
    }

Log in to reply
 

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