1. Given a string, sort it in decreasing order based on the frequency of characters.
  2.  
  3. Example 1:
  4.  
  5. Input:
  6. "tree"
  7.  
  8. Output:
  9. "eert"
  10.  
  11. Explanation:
  12. 'e' appears twice while 'r' and 't' both appear once.
  13. So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
  14. Example 2:
  15.  
  16. Input:
  17. "cccaaa"
  18.  
  19. Output:
  20. "cccaaa"
  21.  
  22. Explanation:
  23. Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
  24. Note that "cacaca" is incorrect, as the same characters must be together.
  25. Example 3:
  26.  
  27. Input:
  28. "Aabb"
  29.  
  30. Output:
  31. "bbAa"
  32.  
  33. Explanation:
  34. "bbaA" is also a valid answer, but "Aabb" is incorrect.
  35. 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. }

Leetcode: Sort Characters By Frequency的更多相关文章

  1. [LeetCode] Sort Characters By Frequency 根据字符出现频率排序

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

  2. 【leetcode】451. Sort Characters By Frequency

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

  3. #Leetcode# 451. Sort Characters By Frequency

    https://leetcode.com/problems/sort-characters-by-frequency/ Given a string, sort it in decreasing or ...

  4. LeetCode 451. Sort Characters By Frequency (根据字符出现频率排序)

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

  5. [LeetCode] 451. Sort Characters By Frequency 根据字符出现频率排序

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

  6. 【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 优先级队列 排序 日期 题目地址:https: ...

  7. [Swift]LeetCode451. 根据字符出现频率排序 | Sort Characters By Frequency

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

  8. 451. Sort Characters By Frequency

    题目: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Inp ...

  9. Sort Characters By Frequency

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

随机推荐

  1. 【BZOJ2038】【2009国家集训队】小Z的袜子(hose) 分块+莫队

    Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...

  2. 给自定义cell赋值代码

    // //  ViewController.m //  11 - 投资管理 - 李洪强 // //  Created by vic fan on 16/4/8. //  Copyright © 201 ...

  3. 1069. The Black Hole of Numbers (20)

    For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...

  4. 收集的github的东西

    1.voghDev/PdfViewPager-Android widget that can render PDF documents stored on SD card, linked as ass ...

  5. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  6. JAVA双向链表

    1.链表是一种重要的数据结构,在程序设计中占有很重要的地位 2.我们可以用类List来实现链表结构,用变量Head.Tail.Length.Pointer来实现表头.存储当前结点的指针时有一定的技 巧 ...

  7. sendfile传输机制

  8. 配置RAC到单节点standby的data guard

    1RAC主库准备 2创建物理备库 3主库调整参数 4测试DG

  9. 使用plupload做一个类似qq邮箱附件上传的效果

    公司项目中使用的框架是springmvc+hibernate+spring,目前需要做一个类似qq邮箱附件上传的功能,暂时只是上传小类型的附件 处理过程和解决方案都需要添加附件,处理过程和解决方案都可 ...

  10. iOS 导入第三方文件夹时右侧出现问号

    首先,和版本库有关. a代表add,m代表modify,?代表未能识别,通常如果使用git之类的版本控制器,添加文件后没有进行提交,就会出现? 1.遇到引用文件夹为蓝色的情况,是你以为勾了copy项, ...