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

Example 1:

  1. Input:
  2. "tree"
  3.  
  4. Output:
  5. "eert"
  6.  
  7. Explanation:
  8. 'e' appears twice while 'r' and 't' both appear once.
  9. So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.

Example 2:

  1. Input:
  2. "cccaaa"
  3.  
  4. Output:
  5. "cccaaa"
  6.  
  7. Explanation:
  8. Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
  9. Note that "cacaca" is incorrect, as the same characters must be together.

Example 3:

  1. Input:
  2. "Aabb"
  3.  
  4. Output:
  5. "bbAa"
  6.  
  7. Explanation:
  8. "bbaA" is also a valid answer, but "Aabb" is incorrect.
  9. Note that 'A' and 'a' are treated as two different characters.
  1. class Solution {
  2. public:
  3. string frequencySort(string s) {
  4. string ans;
  5. vector<pair<char,int>> h;
  6. map<char,int> f;
  7. for(int i=;i<s.size();i++)
  8. {
  9. f[s[i]]++;
  10. }
  11. for(auto it:f)
  12. {
  13. pair<char,int> p(it.first,it.second);
  14. h.push_back(p);
  15. }
  16. sort(h.begin(),h.end(),[](pair<char,int> a,pair<char,int> b){
  17. return a.second>b.second;
  18. });
  19. for(int i=;i<h.size();i++)
  20. {
  21. string t(h[i].second,h[i].first);
  22. ans+=t;
  23. }
  24. return ans;
  25. }
  26. };

Heap-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. LeetCode 451. Sort Characters By Frequency (根据字符出现频率排序)

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

  3. 451. Sort Characters By Frequency

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

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

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

  5. #Leetcode# 451. Sort Characters By Frequency

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

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

    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. [LC] 451. Sort Characters By Frequency

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

  9. 451. Sort Characters By Frequency(桶排序)

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

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

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

随机推荐

  1. 国内maven仓库地址

    Maven 中央仓库地址: 1.http://mvnrepository.com/ (推荐) 2.http://mirrors.ibiblio.org/maven2/ 3.http://repo1.m ...

  2. csv中文乱码

    处理办法:https://jingyan.baidu.com/album/3c48dd3464b46ce10be3581f.html?picindex=2

  3. linux小白

    1. linux下加域名. 文件是在/etc/hosts    中间加的tab键 192.168.0.1 baidu.com linux下测试网页可以用 wget www.baidu.com  这个命 ...

  4. Hibernate不能实时获取MySQL数据库的更新

    在hibernate.cfg.xml配置文件中,增加以下内容: <property name="hibernate.connection.provider_class"> ...

  5. IE浏览器调用jquery需要注意的小问题

    今天在进行前端重构的时候发现了一个非常奇怪的浏览器兼容性问题,我想在网页上放一个JS的特效,于是下载了jquery-easyui,经过修改完成所需要的效果后,准备放入项目中,发现在IE浏览器中无法运行 ...

  6. 构建ASP.NET网站十大必备工具

    最近使用ASP.NET为公司构建了一个简单的公共网站(该网站的地址:http://superexpert.com/).在这个过程中,我们使用了数量很多的免费工具,如果把构建ASP.NET网站的必备工具 ...

  7. 20155233 2016-2017-2 《Java程序设计》第8周学习总结

    20155233 2016-2017-2 <Java程序设计>第8周学习总结 学习目标 了解NIO 会使用Channel.Buffer与NIO2 会使用日志API.国际化 会使用正则表达式 ...

  8. 实现KbmMw web server 支持https

    在以前的文章里面介绍过kbmmw 做web server. 前几天红鱼儿非要我给他做一个支持https 的web server. 其实kbmmw 支持https 有好几种方法: 1. 使用isapi ...

  9. 2018.09.23 atcoder Boxes and Candies(贪心)

    传送门 一道挺有意思的贪心. 从1到n依次满足条件. 注意要特判第一个数已经大于x的情况. 但是如何贪心吃呢? 如果靠左的数没有越界,我们吃靠右的数. 原因是下一次靠右的数就会成为靠左的数,相当于多贡 ...

  10. Django介绍(3)

    https://www.cnblogs.com/yuanchenqi/articles/5786089.html