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. quartz详解4:quartz线程管理

    http://blog.itpub.NET/11627468/viewspace-1766967/ quartz启动后有多个线程同时在跑.启动时会启动主线程.集群线程.检漏线程.工作线程.主线程负责查 ...

  2. 使用docker-sync解决docker for mac 启动的虚拟容器程序运行缓慢的问题

    背景: 新入职的公司有个非常OG的大项目,为了避免新同学重复造轮子,有哥们已经把项目需要的所有打好了一个镜像供我们启动docker. 初次启动docker 使用的命令如下: docker run -i ...

  3. numpy ndarray 打印格式化

    1.ndarray打印省略问题 np.set_printoptions(threshold=np.inf) 2.ndarray打印换行限制 加上下面这句代码,输出时打印不换行 np.set_print ...

  4. JavaWeb之搭建自己的MVC框架(一)

    1. 介绍 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的 ...

  5. XML--XML概览

    参考 https://www.cnblogs.com/fangjian0423/p/xml-namespace.html http://www.w3school.com.cn/x.asp xmlns ...

  6. 13. react 基础 redux 的基本介绍 及 用 antd 编写 TodoList 的样式

    1. redux 简述 当 store 内的 数据进行变更的时候  多个组件感知到 store 内的数据变化 将会被自动更新 2. redux 工作流 Store  代表数据存储 (例如: 图书馆管理 ...

  7. 云服务器Linux版本下---安装git

    xshell进入云服务器: 按照git官网:https://git-scm.com/download/linux   的教程输入: apt-get install git 本地没有包????? 原来是 ...

  8. LCA--P3379 【模板】最近公共祖先(LCA)

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入格式 第一行包含三个正整数 N,M,S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来 N−1 行每行包含两个 ...

  9. Vue-router(5)之 路由的before家族

    beforeEach方法 import Vue from 'vue' import Router from 'vue-router' import Son1 from '@/view/New/son1 ...

  10. 约数个数函数(d)的一个性质证明

    洛谷P3327 [SDOI2015]约数个数和 洛谷P4619 [SDOI2018]旧试题 要用到这个性质,而且网上几乎没有能看的证明,所以特别提出来整理一下. \[ d(AB) = \sum_{x| ...