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. Solution 1: pq
class Solution {
public String frequencySort(String s) {
Map<Character, Integer> map = new HashMap<>();
for (Character c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
PriorityQueue<Map.Entry<Character, Integer>> pq = new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
pq.addAll(map.entrySet());
char[] charArr = new char[s.length()];
int i = 0;
while (i < charArr.length) {
Map.Entry<Character, Integer> entry = pq.poll();
Character cur = entry.getKey();
Integer times = entry.getValue();
int j = 0;
while (j < times) {
charArr[i + j] = cur;
j += 1;
}
i += times;
}
return new String(charArr);
}
}

Solution 2: bucket sort based on Freqency

class Solution {
public String frequencySort(String s) {
Map<Character, Integer> map = new HashMap<>();
for (Character c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
// have a freq buckets from 0 to s.length, 0 is unused
List<Character>[] buckets = new List[s.length() + 1];
for (char c : map.keySet()) {
int times = map.get(c);
if (buckets[times] == null) {
buckets[times] = new LinkedList<>();
}
buckets[times].add(c);
}
StringBuilder sb = new StringBuilder();
for (int i = buckets.length - 1; i >= 0; i--) {
if (buckets[i] != null) {
for (char ch: buckets[i]) {
for (int j = 0; j < i; j++) {
sb.append(ch);
}
}
}
}
return sb.toString();
}
}

[LC] 451. Sort Characters By Frequency的更多相关文章

  1. 【leetcode】451. Sort Characters By Frequency

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

  2. 451. Sort Characters By Frequency将单词中的字母按照从高频到低频的顺序输出

    [抄题]: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: I ...

  3. 451. Sort Characters By Frequency

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

  4. #Leetcode# 451. Sort Characters By Frequency

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

  5. 451. Sort Characters By Frequency (sort map)

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

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

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

  7. [LeetCode] 451. 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: Input: ...

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

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

随机推荐

  1. PAT 2018 秋

    A 1148 Werewolf - Simple Version 思路比较直接:模拟就行.因为需要序列号最小的两个狼人,所以以狼人为因变量进行模拟. #include <cstdio> # ...

  2. JSON整理

    1.什么是JSON JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. 2.JSON基于两种结构: (1 )“名称/值“对的集合(A co ...

  3. python 流行库、库的基本用法

    进入github,输入python 点击see topic 进入python流行的库  链接 https://github.com/topics/python 1.QuantLib 金融衍生品数据库 ...

  4. 18 12 25 css 基本语法以及页面使用

    css的定义方法是: 选择器 { 属性:值; 属性:值; 属性:值;} 选择器是将样式和页面元素关联起来的名称,属性是希望设置的样式属性每个属性有一个或多个值 css页面引入方法: 1.外联式:通过l ...

  5. ubuntu root用户下找不到环境变量解决办法

    打开 gedit /root/.bashrc   ,在文件的末尾添加: source /etc/profile 然后执行更新:source /root/.bashrc

  6. ZOJ 3299 线段树 离散化

    本来是个很简单的题目,难住我的主要是这么几点 1.它所有的点都是坐标,不是实际的砖块,1,3指的是1-2 2-3的砖块...后来就是用1 代表1-2 ,2代表2-3.....,这样的话,每次读入的数据 ...

  7. MySQL数据库索引常见问题

    笔者看过很多数据库相关方面的面试题,但大多数答案都不太准确,因此决定在自己blog进行一个总结. Q1:数据库有哪些索引?优缺点是什么? 1.B树索引:大多数数据库采用的索引(innoDB采用的是b+ ...

  8. springcloud之Eureka上

    0 环境 系统环境:win10 编辑器:IDEA 1 注册中心 Eureka是springcloud中的注册中心.原因: 当是单体应用 类似一条直线 随着项目越来越大 系统拆分 类似那个藕(模块间相互 ...

  9. 在excel中评估模型性能

    一直在用的结果, 从代码中整理出来. 评分卡模型的结果一般在excel中即可计算完成. 下面是在number中计算评分卡模型的性能(KS/AUC), 表格中百分数省略%

  10. vue简单逻辑判断

    条件判断能否显示 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...