Leetcode: 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
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的更多相关文章
- [LeetCode] Sort Characters By Frequency 根据字符出现频率排序
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- 【leetcode】451. Sort Characters By Frequency
Given a string s, sort it in decreasing order based on the frequency of the characters. The frequenc ...
- #Leetcode# 451. Sort Characters By Frequency
https://leetcode.com/problems/sort-characters-by-frequency/ Given a string, sort it in decreasing or ...
- LeetCode 451. Sort Characters By Frequency (根据字符出现频率排序)
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- [LeetCode] 451. Sort Characters By Frequency 根据字符出现频率排序
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- 【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 优先级队列 排序 日期 题目地址:https: ...
- [Swift]LeetCode451. 根据字符出现频率排序 | Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- 451. Sort Characters By Frequency
题目: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Inp ...
- Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
随机推荐
- JS:操作样式表1:行内样式
//访问元素样式1, stye属性只对行内样式有用 var box = document.getElementById("box"); // alert(box.style.col ...
- IOS 网络请求方式
iOS开发中的网络请求 今天来说说关于iOS开发过程中的网络请求. 关于网络请求的重要性我想不用多说了吧.对于移动客户端来说,网络的重要性不言而喻.常见的网络请求有同步GET, 同步POST, 异 ...
- springmvc项目中java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener 严重: Error co ...
- [LintCode] House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- #1045 - Access denied for user 'root'@'localhost' (using password: NO)
解决办法 打开 phpmyadmin里的config.inc.php文件 $cfg['Servers'][$i][ 'host'] = 'localhost';$cfg['Servers'][$i][ ...
- spring security方法一 自定义数据库表结构
Spring Security默认提供的表结构太过简单了,其实就算默认提供的表结构很复杂,也无法满足所有企业内部对用户信息和权限信息管理的要求.基本上每个企业内部都有一套自己的用户信息管理结构,同时也 ...
- 执行JDBC的executeUpdate()方法时,报错:数据类型不一致,应为number,但却为binary
该原因是因为,在拼写update语句的时候将一个number类型的字段的值赋为了null导致的,如果想将一个number类型的字清空,不能使用null,可以使用“”来替代.
- C#_项目做成安装包
首先打开项目-->点开解决方案右键单击添加-->新建项目-->找到其他项目类型(如下图)--> 点击确定-->单击应用程序文件夹-->在名称下的空白处单击--> ...
- EDI - Biztalk Setting
1. Applications:
- FastDFS connect timed out
java.net.SocketTimeoutException: connect timed outUpload file "1003.png"fails:connect time ...