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. spring boot 生命周期初探

    1.MytestApplication package com.gomepay; import org.springframework.boot.Banner; import org.springfr ...

  2. VUE.js入门学习(3)-深入理解VUE组建

    1.使用组件的细节点 (1)is="模版名" (2)在子组建定义data的时候,data必须是一个函数,而不能是一个对象,每个子组建都有自己的数据存储.之间不会相互影响. (3)操 ...

  3. Tensorflow——用openpose进行人体骨骼检测

    https://blog.csdn.net/eereere/article/details/80176007 参考资料code:https://github.com/ildoonet/tf-pose- ...

  4. vector删除指定元素

    #pragma once #include "stdafx.h" #include<windows.h> #include <vector> #includ ...

  5. git push的时候.gitignore不起作用的解决方法

    问题的原因 这是因为在你添加.gitignore之前已经进行过push操作,有些文件已经纳入版本管理了. 解决方法 我们就应该先把本地缓存删除,然后再进行git的push,这样就不会出现忽略的文件了. ...

  6. Linux无法连接网络解决方案

    上次在VM中装好Linux以后,用xshell可以连接上Linux,可是今天在启动虚拟机打开Linux以后,发现又没有网络连接了,因为要用xshell连接的话首先要知道Linux的ipv4地址,在li ...

  7. 洛谷P1002 过河卒(动态规划)

    题目描述 棋盘上 AA 点有一个过河卒,需要走到目标 BB 点.卒行走的规则:可以向下.或者向右.同时在棋盘上 CC 点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为 ...

  8. RNA组研究困难

    RNA组研究的困难何在?如果开发新技术来解决这些困难,您最想解决的科学问题是什么? RNA研究的困难在于研究技术落后 (1)从信息流来说,我们需要直接测定RNA的序列,但是我们只能DNA测序仪间接测得 ...

  9. OA|DOAJ|Highwire press|Springeropen|Plos journal|电子印本|中国科技论文在线|arxiv|chinaxiv|MIT机构知识库|中科院机构知识库|Email alert|Citeseer|RSS|F1000 prime

    信息检索 OA:open access开放获取 金色OA:出版社主导, 开放出版,全部都可以下载. 开放论文:只有部分可以下载. 绿色OA:作者主导,发表后放在机构知识库中,排版不同,但是内容一致.E ...

  10. IP首部检验和的计算和举例

    IP首部校验和 首部校验和(16位)字段只检验数据报的首部,不检验数据部分.这里不采用CRC检验码而采用简单的计算方法. 发送端 首先将检验和置零,求首部数据的补码和(包含检验和),因为为零,所以无影 ...