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

时间:2022-05-06 09:46:42

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. &lbrack;LeetCode&rsqb; Sort Characters By Frequency 根据字符出现频率排序

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

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

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

  4. &num;Leetcode&num; 451&period; Sort Characters By Frequency

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

  5. 【leetcode】451&period;&&num;160&semi;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&period; Sort Characters By Frequency 解题报告(Python & C&plus;&plus;)

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

  7. 451&period; Sort Characters By Frequency

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

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

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

  9. 451&period; Sort Characters By Frequency &lpar;sort map&rpar;

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

随机推荐

  1. java基础-注释

    注释是一种形式的元数据,提供了非程序自身的数据,注释对于被注释的代码没有直接的影响. 本文主要概括注释的使用,java平台(SE)预定义的注释,类型注释是如跟可插入类型系统连用达到更强的类型检查的,以 ...

  2. Windows Server 2008 R2 主域控制器委派DNS到子域控控制器

    一.实验网络拓扑图: 二.实现目的: 子域控制器的域用户能查询到主域控制器的DNS服务器的A记录,主域控制器的域用户也能查询到子域控制器的DNS服务器的A记录. 标注:此章节不讲解域控和DNS服务器的 ...

  3. TestLink学习六:TestLink1&period;9&period;13工作使用小结

    Testlink是一款强大的用例追踪和管理工具.测试管理注重的实际上就是一个流程. 1.默认当测试用例同名时,就会有提示.(以前版本需要修改配置) 2.测试用例序号:(缺点) 1)删除一个测试用例之后 ...

  4. PowerShell - Read an Excel file using COM Interface

    http://www.lazywinadmin.com/2014/03/powershell-read-excel-file-using-com.html

  5. bootstrap学习笔记&lt&semi;四&gt&semi;(table表格)

    表格 bootstrap为table表格定制多个常用样式:基本样式,隔行变色样式,带边框样式,荧光棒样式,紧凑样式,响应样式. ☑  .table:基础表格 ☑  .table-striped:斑马线 ...

  6. tomcat之 Tomcat 7&period;0&period;78 单机多实例配置

    前言:JDK(JavaDevelopment Kit)是Sun Microsystems针对Java开发员的产品.自从Java推出以来,JDK已经成为使用最广泛的javaSDK. JDK是整个Java ...

  7. C&num;中的集合之ArryList

    List泛型集合 集合是OOP中的一个重要概念,C#中对集合的全面支持更是该语言的精华之一. 为什么要用泛型集合? 在C# 2.0之前,主要可以通过两种方式实现集合: a.使用ArrayList 直接 ...

  8. struts2:表单标签

    目录 表单标签1. form标签2. submit标签3. checkbox标签4. checkboxlist标签5. combobox标签6. doubleselect标签7. head标签8. f ...

  9. POJ 1511 Invitation Cards &sol; UVA 721 Invitation Cards &sol; SPOJ Invitation &sol; UVAlive Invitation Cards &sol; SCU 1132 Invitation Cards &sol; ZOJ 2008 Invitation Cards &sol; HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

  10. 基于flex的不定个数的按钮组

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...