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.

给一个字符串按照字符出现的频率来排序。

Java:

public class Solution {
public String frequencySort(String s) {
HashMap<Character, Integer> charFreqMap = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
charFreqMap.put(c, charFreqMap.getOrDefault(c, 0) + 1);
}
ArrayList<Map.Entry<Character, Integer>> list = new ArrayList<>(charFreqMap.entrySet());
list.sort(new Comparator<Map.Entry<Character, Integer>>(){
public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
StringBuffer sb = new StringBuffer();
for (Map.Entry<Character, Integer> e : list) {
for (int i = 0; i < e.getValue(); i++) {
sb.append(e.getKey());
}
}
return sb.toString();
}
} 

Python:

class Solution(object):
def frequencySort(self, s):
"""
:type s: str
:rtype: str
"""
return ''.join(c * t for c, t in collections.Counter(s).most_common()) 

Python:

class Solution(object):
def frequencySort(self, s):
"""
:type s: str
:rtype: str
"""
freq = collections.defaultdict(int)
for c in s:
freq[c] += 1 counts = [""] * (len(s)+1)
for c in freq:
counts[freq[c]] += c result = ""
for count in reversed(xrange(len(counts)-1)):
for c in counts[count]:
result += c * count return result

C++:

class Solution {
public:
string frequencySort(string s) {
unordered_map<char, int> freq;
for (const auto& c : s) {
++freq[c];
} vector<string> counts(s.size() + 1);
for (const auto& kvp : freq) {
counts[kvp.second].push_back(kvp.first);
} string result;
for (int count = counts.size() - 1; count >= 0; --count) {
for (const auto& c : counts[count]) {
result += string(count, c);
}
} return result;
}
};

  

  

All LeetCode Questions List 题目汇总

[LeetCode] 451. Sort Characters By Frequency 根据字符出现频率排序的更多相关文章

  1. 451 Sort Characters By Frequency 根据字符出现频率排序

    给定一个字符串,请将字符串里的字符按照出现的频率降序排列.示例 1:输入:"tree"输出:"eert"解释:'e'出现两次,'r'和't'都只出现一次.因此' ...

  2. [LeetCode] Sort Characters By Frequency 根据字符出现频率排序

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

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

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

  4. #Leetcode# 451. Sort Characters By Frequency

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

  5. 【leetcode】451. Sort Characters By Frequency

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

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

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

  7. 451. Sort Characters By Frequency

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

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

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

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

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

随机推荐

  1. 【转】什么时候 i = i + 1 并不等于 i += 1?

    增强型赋值语句是经常被使用到的,因为从各种学习渠道中,我们能够得知 i += 1 的效率往往要比 i = i + 1 更高一些(这里以 += 为例,实际上增强型赋值语句不仅限于此). 所以我们会乐此不 ...

  2. web scraper——爬取知乎|微博用户数据模板【三】

    前言 在这里呢,我就只给模板,不写具体的教程啦,具体的可以参考我之前写的博文. https://www.cnblogs.com/wangyang0210/p/10338574.html 模板 进入微博 ...

  3. Dubbo源码分析:Invoker

    背景 调用对象!在调用过程可以使用Filter接口方法.Inovoker调用过程采用了装饰者设计模式.Filter最后一个ExcpetionFilter对象,这个对象之后就调用服务方法.服务对象是配置 ...

  4. Spark Partition

    分区的意义 Spark RDD 是一种分布式的数据集,由于数据量很大,因此它被切分成不同分区并存储在各个Worker节点的内存中.从而当我们对RDD进行操作时,实际上是对每个分区中的数据并行操作.Sp ...

  5. 域权限维持:Ptk(Pass The Ticket)

    Kerberos协议传送门:https://www.cnblogs.com/zpchcbd/p/11707302.html 1.黄金票据 2.白银票据

  6. LeetCode 801. Minimum Swaps To Make Sequences Increasing

    原题链接在这里:https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/ 题目: We have two in ...

  7. WinDbg常用命令系列---?*

    ? (Command Help) 问号(?)字符显示所有命令和运算符的列表.问号本身显示命令帮助. 环境 模式 用户模式下,内核模式 目标 实时. 崩溃转储 平台 全部 0:000> ? Ope ...

  8. WinDbg扩展

    WinDbg的扩展,也可以叫插件.它用于实现针对特定调试目标的调试功能,用于扩展某一方面的调试功能.扩展的实现是通过扩展模块(DLL)实现的.Windbg本身已经包含了很多扩展命令,这些扩展为这Win ...

  9. Bzoj 1927: [Sdoi2010]星际竞速(网络流)

    1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MB Description 10年一度的银河系赛车大赛又要开始了.作为全银河最盛大 ...

  10. sqlalchemy lock and atomic

    prepare: MYSQL tutorial Prepare a table set evn DBUSER=root DBPASS= DBNAME=cyborgTBNAME="atomic ...