Java Accepted Priority Queue solution


  • 0
    M
        public String frequencySort(String s) {
            if(s == null || s.isEmpty()) {
                return "";
            }
            int[] arr = new int[256];
            for(char c : s.toCharArray()) {
                arr[c]++;
            }
            
            PriorityQueue<Pair> pq = new PriorityQueue<>(s.length(), new Comparator<Pair>() {
                public int compare(Pair p, Pair q) {
                    return q.freq - p.freq;
                } 
            });
            
            for(int i = 0; i < 256; i++) {
                if(arr[i] > 0) {
                    pq.offer(new Pair((char)i , arr[i]));
                }
            }
            
            StringBuilder sb = new StringBuilder();
            while(!pq.isEmpty()) {
                Pair p = pq.poll();
                while(p.freq != 0) {
                    sb.append(p.c);
                    p.freq--;
                }
            }
            return sb.toString();
        }
        
        class Pair {
            int freq;
            char c;
            
            public Pair(char c, int freq) {
                this.c = c;
                this.freq = freq;
            }
        }
    

Log in to reply
 

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