Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Sort Characters By Frequency problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Sort Characters By Frequency problem solution, we have given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string.

Return the sorted string. If there are multiple answers, return any of them.

Leetcode Sort Characters By Frequency problem solution

Leetcode Sort Characters By Frequency problem solution in Python.

class Solution:
    def frequencySort(self, s: str) -> str:
        dic=collections.defaultdict(int)
        for st in s:dic[st]+=1
        return ''.join(sorted(s,key=lambda x: (dic[x],ord(x)),reverse=True))

Sort Characters By Frequency problem solution in Java.

class Solution {
    public String frequencySort(String s) {
        Map<Character,Integer>map=new TreeMap<>();
        for(char ch:s.toCharArray()){
            map.put(ch,map.getOrDefault(ch,0)+1);
        }
        PriorityQueue<Pair>pq=new PriorityQueue<Pair>((obj1,obj2)->obj2.frequency-obj1.frequency);
        for(Map.Entry<Character,Integer>entry:map.entrySet()){
            pq.add(new Pair(entry.getKey(),entry.getValue()));
        }
        StringBuilder sbil=new StringBuilder();
        while(!pq.isEmpty()){
            Pair obj=pq.poll();
            char c=obj.ch;
            int f=obj.frequency;
            for(int i=0;i<f;i++){
                sbil.append(c+"");
            }
        }
        return sbil.toString();
    }
    class Pair{
        int frequency;
        char ch;
        public Pair(char ch,int frequency){
            this.ch=ch;
            this.frequency=frequency;
        }
    }
}

Problem solution in C++.

string frequencySort(string s) {
    unordered_map<char, int>mp;
    for (auto i : s) {
        mp[i]++;
    }
    priority_queue<pair<int, char>>pt;
    for (auto i : mp) {
        pt.push({ i.second,i.first });
    }
    string ans = "";
    while (!empty(pt)) {
        int val = pt.top().first;
        while (val) {
            ans += pt.top().second;
            val--;
        }
        pt.pop();
    }
    return ans;

}

Problem solution in C.

typedef struct
{
    int count;
    char ch;
}Map;

int cmp(const void * a, const void * b)
{
    return ((Map *)b)->count - ((Map *)a)->count;
}

char * frequencySort(char * s){
    Map node[125] = {0};
    int idx = 0;
    for(int i=0; i<strlen(s); i++)
    {
        node[s[i]].count++;
        node[s[i]].ch = s[i];
    }
    qsort(node, 125, sizeof(Map), cmp);
    char * result = (char *)calloc(strlen(s)+1, sizeof(char));
    for(int i=0; i<125; i++)
    {
        if(node[i].count != 0)
        {
            for(int j=0; j<node[i].count; j++)
                result[idx++] = node[i].ch;
        }
        else break;
    }
    return result;
    
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes