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

 public class Solution {
public String frequencySort(String s) {
Map<Character, Integer> map = new HashMap<>();
for (char c : s.toCharArray()) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
List<Character> [] bucket = new List[s.length() + 1];
for (char key : map.keySet()) {
int frequency = map.get(key);
if (bucket[frequency] == null) {
bucket[frequency] = new ArrayList<>();
}
bucket[frequency].add(key);
}
StringBuilder sb = new StringBuilder();
for (int pos = bucket.length - 1; pos >=0; pos--) {
if (bucket[pos] != null) {
for (char num : bucket[pos]) {
for (int i = 0; i < map.get(num); i++) {
sb.append(num);
}
}
}
}
return sb.toString();
}
}

HashMap+ Heap+Wrapper Class

 public class Solution {
public String frequencySort(String s) {
PriorityQueue<WrapperChar> maxheap = new PriorityQueue(1, new Comparator<WrapperChar>() {
public int compare(WrapperChar w1, WrapperChar w2) {
return w2.count - w1.count;
}
});
HashMap<Character, WrapperChar> map = new HashMap<Character, WrapperChar>();
for (int i=0; i<s.length(); i++) {
char c = s.charAt(i);
if (!map.containsKey(c)) map.put(c, new WrapperChar(c, 1));
else {
int newCount = map.get(c).count + 1;
map.put(c, new WrapperChar(c, newCount));
}
}
for (char c : map.keySet()) maxheap.offer(map.get(c));
StringBuilder res = new StringBuilder();
while (!maxheap.isEmpty()) {
WrapperChar wChar = maxheap.poll();
for (int i=0; i<wChar.count; i++) {
res.append(wChar.c);
}
}
return res.toString();
} public class WrapperChar {
char c;
int count;
public WrapperChar(char ch, int num) {
this.c = ch;
this.count = num;
}
}
}

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. 【BZOJ2473/2120】维护队列 分块+二分

    Description 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会 ...

  2. LINQ to Entities 和LINQ to Objects 的区别

    本文资料来源:http://www.codeproject.com/Articles/246861/LINQ-to-Entities-Basic-Concepts-and-Features) LINQ ...

  3. exp.validate.js

    简单实用的js基础数据验证 prototype /// <reference path="/Scripts/expand-fn/exp_validate.js" /> ...

  4. ubuntu 12.04 安装wireshark

    轉載自http://blog.chinaunix.net/uid-27064719-id-3786626.html 在ubuntu 12.04下安装wireshark软件之后,打开wireshark开 ...

  5. Erlang及Rabbitmq安装

    1. 下载erlang源代码及RabbitMQ rpm安装包      $ wget http://www.erlang.org/download/otp_src_R16B02.tar.gz $ wg ...

  6. javascript问题积累

    今天在写网页时碰到了几个js可以解决的小问题,很好用,很简便 1.鼠标移动到图片上时可更换图片,比如用到给图片加颜色,去颜色. <img src="../img/02.gif" ...

  7. ListView的HeaderView和Footer

    HeaderView介绍 HeaderView用法 属性中添加 ListView中属性listHeader和overScrollHeader区别: android:overScrollHeader=& ...

  8. js写当鼠标悬浮及移开出现背景变化

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. ubuntn 安装 MySQL

    1. sudo apt-get install mysql-server   2. apt-get isntall mysql-client   3.  sudo apt-get install li ...

  10. php课程---建立一个简单的下拉列表框

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...