Leetcode 451. Sort Characters By Frequency 按频率对字符排序

Given a string, sort it in decreasing order based on the frequency of characters.

Example 1:

Input:
"tree"

Output:
"eert"

Explanation:
'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.

Example 2:

Input:
"cccaaa"

Output:
"cccaaa"

Explanation:
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
Note that "cacaca" is incorrect, as the same characters must be together.

Example 3:

Input:
"Aabb"

Output:
"bbAa"

Explanation:
"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters.

Bucket Sort + HashMap

复制代码
 1 public class Solution {
 2     public String frequencySort(String s) {
 3         Map<Character, Integer> map = new HashMap<>();
 4         for (char c : s.toCharArray()) {
 5             if (map.containsKey(c)) {
 6                 map.put(c, map.get(c) + 1);
 7             } else {
 8                 map.put(c, 1);
 9             }
10         }
11         List<Character> [] bucket = new List[s.length() + 1];
12         for (char key : map.keySet()) {
13             int frequency = map.get(key);
14             if (bucket[frequency] == null) {
15                 bucket[frequency] = new ArrayList<>();
16             }
17             bucket[frequency].add(key);
18         }
19         StringBuilder sb = new StringBuilder();
20         for (int pos = bucket.length - 1; pos >=0; pos--) {
21             if (bucket[pos] != null) {
22                 for (char num : bucket[pos]) {
23                     for (int i = 0; i < map.get(num); i++) {
24                         sb.append(num);
25                     }
26                 }
27             }
28         }
29         return sb.toString();
30     }
31 }
复制代码

HashMap+ Heap+Wrapper Class

复制代码
 1 public class Solution {
 2     public String frequencySort(String s) {
 3         PriorityQueue<WrapperChar> maxheap = new PriorityQueue(1, new Comparator<WrapperChar>() {
 4             public int compare(WrapperChar w1, WrapperChar w2) {
 5                 return w2.count - w1.count;
 6             }
 7         });
 8         HashMap<Character, WrapperChar> map = new HashMap<Character, WrapperChar>();
 9         for (int i=0; i<s.length(); i++) {
10             char c = s.charAt(i);
11             if (!map.containsKey(c)) map.put(c, new WrapperChar(c, 1));
12             else {
13                 int newCount = map.get(c).count + 1;
14                 map.put(c, new WrapperChar(c, newCount));
15             }
16         }
17         for (char c : map.keySet()) maxheap.offer(map.get(c));
18         StringBuilder res = new StringBuilder();
19         while (!maxheap.isEmpty()) {
20             WrapperChar wChar = maxheap.poll();
21             for (int i=0; i<wChar.count; i++) {
22                 res.append(wChar.c);
23             }
24         }
25         return res.toString();
26     }
27     
28     public class WrapperChar {
29         char c;
30         int count;
31         public WrapperChar(char ch, int num) {
32             this.c = ch;
33             this.count = num;
34         }
35     }
36 }
复制代码



class Solution {
    class MyComparator implements Comparator {

        @Override
        public int compare(Object o1, Object o2) {
            Map.Entry e1 = (Map.Entry) o1;
            Map.Entry e2 = (Map.Entry) o2;

            // Reverse
            return (int)e2.getValue() - (int)e1.getValue();
        }
    }

    public String frequencySort(String s) {
        Map<String, Integer> mp = new HashMap<>();
        for (int i=0; i<s.length(); i++) {
            String key = s.substring(i, i+1);
            if (mp.containsKey(key)) {
                mp.put(key, mp.get(key)+1);
            }
            else {
                mp.put(key, 1);
            }
        }

        List<Map.Entry> lst = new ArrayList<>(mp.entrySet());
        Collections.sort(lst, new MyComparator());
        Iterator<Map.Entry> iter = lst.iterator();
        StringBuilder sb = new StringBuilder();
        while (iter.hasNext()) {
            Map.Entry entry = iter.next();
            for (int j=0; j<(int)entry.getValue(); j++) {
                sb.append(entry.getKey());
            }
        }
        return sb.toString();
    }
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值