 What's up guys my is Michael and welcome to my YouTube channel today We're gonna do another lead code challenge called sort characters by frequency Basically given a string sort it decreasing order based on the frequency of the characters So this is actually really easy Because it's decreasing order Okay, so first you got to get the frequency of each character after that we're going to basically add Each frequency and the character into a heap and the heap is going to be a Max heap because it's decreasing order right the maximum is first and it's decreasing order after we finish adding all the values to the Max heap we're just going to iterate through a max heap and then add each character That many times to the corresponding Frequency it was okay to a to our string So we're gonna do that right now. This is actually pretty quick easy quick and dirty I'm gonna call use a map to get the frequency of each character and This is going to be called Freak. Okay. I'm gonna go through every care character CH inside our string Yeah, we need frequency at CH plus plus so for every character in our string frequency plus plus So that's gonna give us our frequency of each character in our string Then after that I need to create a max heap to represent I'm gonna pair each Frequency with its corresponding character in our max heap So in our max it was continued pairs of frequency and the character So now vector pair int char Yeah, and because it's a max heap we don't have to pass in our comparator or anything It's a max heap because priority queues are already max heaps Okay, and now and go through now. We're gonna go through every value in our frequency list Of the character in the integer for each character each integer So I'm gonna see every value each val in our frequency list I think you should make this an ampersand just in case I think I just I just see people do that. So Yeah So each value We're gonna do max heap dot push each value Yeah, each value then we're gonna do the first value is gonna be the occurrences and then the second one's gonna be Each character right so we actually want to push So in our frequency we have we mapped each character to its occurrences right The free how frequent the character was inside our string So we're actually gonna have to pair up the opposite way second first the reason why we're doing this is because each Are each value our pair our max heap compares based on the Frequency of each character the actual frequency not the character. So we have to do each character first a way you could avoid the swapping the Way you could avoid like flipping these two is actually to pass in your own comparator So then given the pair you could do like Instead of doing int char you could do char int and then pass in a comparator and then you compare the specific int value But I'm not doing that rather just do this. Okay, so then we're gonna push each value to our heap and then now now That's basically it I'm gonna do while max heap Size of actually I should do is not empty I Feel like that's much cleaner instead of checking if the size is greater than zero. Yeah Well, it's not empty. I'm gonna get the top value So the top value is a pair of So the first value is going to be the frequent how frequent the character is so how frequent character is Is the first value in our pair? All right, this is how frequent the character in our max heap first one is how frequent the character is second one is the actual character, so here I'm gonna do character Max heap Dot top dot second. Okay, so now we have the how frequent the character and character Now because our max heap is sorted decreasing order based on the frequency of the characters We just have to go through our max heap and then for every For every value in our max heap We're just gonna have to add it to our new string that we're returning. So string to return. I'm gonna make it empty So now I'm going to basically just loop through For every I equals your eyes less than how frequent character Plus And I'm just going to add to return plus equal to character Yeah, after that I got to remove the first value of our max heap every time Okay, so that would get rid of That would get rid of that make sure that matter we're moving the first value of our max heap every time So then it'll become empty in the end. So yeah, that's how you do it and now we're going to return to return submit And it got accepted. So yeah, that's how you would do this Ray com subscribe. It wasn't that much difficult So, yeah, that was it. Hope you guys enjoy this video Ray com subscribe. I'll check you guys later. Peace