 What's up guys nice Michael and welcome to my YouTube channel today. We're gonna do the the question reorganized string So I actually tried to make this video once already and it didn't work out Cuz like I kept stuttering and try to couldn't really understand what I was doing But I actually don't understand what I'm doing And I actually couldn't figure out this question for the past two days and I was stuck on it I had to look at the solution, but now I know what what how to do this question. Okay, so This question basically asks given a string check if the letters can be arranged so that two characters that are Jason See each other are not the same. So in the case of the example one a a b A and a are the same characters and they're adjacent to each other next to each other you want to reorganize it so that these Characters that are the same are not adjacent to each other So in this case the output would become a b a because they swapped B And they put B in between the two adjacent letters a b a In a second example, it's not possible. That's why they return in They return a empty string the reason why is because in this case a a a b We've if you take B and you put it between the first two a's The last two a's are going to be adjacent to each other and they're the same so that wouldn't work It might take B and put the last two a's the first two a's are going to be adjacent to each other and That wouldn't work either. Okay So how do you do this question? Well, the way to do this question is actually to use a heap And so if you were to count the occurrences of every single letter inside your string Then after you count the occurrences and you sort you pair every occurrences with each character and Then you put it in a heap Based on that heap if you take the two topmost values of that heap and you you were to write Add those two top new values into your new string you would basically have No adjacent characters that are the same next to each other because in the end they're not going to be the same You're taking the two topmost values and then you're gonna add them to your string If you keep doing that, they're not gonna you're not gonna have adjacent Characters that are the same because you're just doing like a B a B still like that. Yeah, so how do you do this? Well first thing you got to know is that We have a string s so we're gonna create a we're gonna not a we're gonna create a length n for s.size just in case if we need it because we need it basically and we need it We are going to basically Keep track of every letter That occurred in the alphabet in our string. So we're gonna do this called count and it's gonna have 26 because there's 26 letters in our alphabet and because that we're gonna have Initialize all the values to zero in the beginning Now what are we gonna do? We're gonna go through every character X maybe ch in our string and we are going to Add the care and the number of occurrences for each character. Okay, so the issue with this is that? the character a b c d e f g h i j k up to z is a Character that has an ASCII value. We want to change that ASCII value to start to zero because that's how our count indexes index from so we have to do minus a And that's going to add the number of occurrences for each letter in our alphabet in our count Okay, so after you do that We need to create a priority queue and it's gonna be our heap Priority queue and It's gonna be a pair of int in character because repairing the number of occurrences with its character And we're gonna call this a vector and that vector is going to be a pair Okay I'm gonna call this PQ priority queue Maybe I should call it priority Priority queue priority queue. Yeah, I will do that. I'll call it priority queue. Thank you. Okay I'm not gonna write the comparator yet. I'll write it later because that's gonna be hard to grasp your head around for the comparator, but I'll write it later. Okay So, what are we gonna do? We're gonna go through every every value in our array of Count which is 26 26 letters in the alphabet that we had a count and we're gonna check if count is greater than zero Okay, so if it's greater than zero we are we're gonna check if you can actually pair the two Values are not the same. So if you look at our string here string s is equal to a B If you were to pair these two values up together together, right? You're gonna have a ba, right the ones are not repeating with the ones that are repeating The size of this is n Let's say the size is n And over two would be the number of groups that you're pairing with each other, right? So therefore the upper bound Is gonna be n plus one over two and if your number of currencies is greater than n plus one over two That means it's not possible because you cannot have pairs like that cannot pair each Same character with this adjacent. So we're gonna have to do that here So we're gonna do if our count at I is greater than n plus one over two We're gonna return empty string. Okay, so After that So after this if statement we know that it's not The count at I is not is greater than n plus one over two So what we're gonna do is we're going to actually add the values to our priority queue So we're gonna do priority queue dot push and then we're gonna add a pair We're gonna add the specific count, which is the the number of occurrences that came and then the actual character so to do that I have to Convert the specific character into an actual character. So to do that, I'll do I Plus a and that will convert the specific index into an actual character So that would that's gonna do that part. Okay. Now we're gonna do another part So I'm gonna check while our priority queue size Is greater than We equal to two the reason we're doing this is because we're gonna take the two topmost values And then we're gonna add it to our string that we're returning. So we're gonna do string to return equals empty So we're gonna take the top two values in our priority queue. So we're gonna do priority queue dot top And this is gonna be pair into char Let's see pair one. I'll call it pair one top I'm gonna remove it To get the second one because I only have the access to the first element. So a pair Remove the pop it now. I'm gonna get the second one pair two and I'm gonna remove it also. Okay So Once we get the top values, we're going to actually specifically add the specific character into the string that we're returning So we're gonna do to return plus equal to pair one dot second Then we're gonna do to return plus equal to pair two dot second and that will actually add up the specific character We know if we do dot second because for pairs dot first is the first five second was a Second value. So dot second is our actual character. So that's where we do that Okay now after we add the character to our string to return we need to check if We have to keep adding the best specific value so We need to check if we have to add the value again into our priority queue So to do that, we have to check if the number of occurrences subtracted one If it's if it's still greater than zero We are going to push the pair back into our priority queue in order to make sure that we add the values again and When you push that into your priority queue, it'll make sure that you have added again This would make sure that the number of occurrences you keep adding the characters so that the number of occurrences would Make sense. So in this case of a a b a has a number of currents is to be as number of currents is one If I were to subtract one from number of currencies So a is a number of currencies is to subtract one is gonna be one So is gonna have one I'm gonna push a again into the priority queue to make sure that I add a again To my to return string So I'm gonna do that in this part. So I'm gonna do if pair one dot first Minus-minus. So I'm gonna do a pre-subtraction. So this would subtract one from the value and set equal to the actual pair is greater than zero I'm gonna do a Priority queue dot push pair one and I have to do the same thing for the second value. So I have to do second pair So pair two dot first is greater than zero Priority queue dot Push pair two. Okay. So once you add those That's basically that part we also have to check There's also another case where if you have like odd number of characters, you're gonna have one less in the priority queue You're gonna have like a one remaining one. So we have to do that educate also. So if priority queue Dot size is still greater than zero. That means there's one left We're going to add it to our to return Dot second. Oh dot top and then the character that specific character dot second Add the specific character to our priority to our string that returning for our priority queue and then we're gonna remove the last Priority queue. Okay, so that's gonna that be that part and Yeah, that's that's pretty much this Does pretty much it then after that we have to return our to return Which is our string that we're returning. So I'm gonna move return to return and that returns the string Okay, so now it's a part of actually adding the comparator for priority queue to know which actually goes first and which actually goes second and Whether the minimum goes first the maximum goes first yada yada yada So I'm gonna call this one comparator I'm gonna pass a comparator here, and then I have to make sure it's a comparator Also here There's a thing we got to do dex cl type if you're gonna use a lambda expression and I'm gonna actually use a lambda because I Don't want to create a struct just to pass in a comparator. So That's that part. I'm gonna do auto comparator is gonna equal to this function and I think you have to have a semicolon if I recall. Yeah Okay, the functions are going to take in a pair of int char and we're gonna call this left and then pair int char Right so We have to think about this We're gonna return if the number of occurrences are the same So if left dot if the number of occurrences are the same, so let's say I have a is equal to two and B is equal to two we want the lower value of the Alphabetic order of the characters are here first. So if first is equal to right dot first We want the lower value of the character You we want the lower value of the alpha you we want it to be sorted in Alphabetical order from the lower value first. So we want the minimum lower value. So we want a to go first so Yeah, to write a comparator. It's actually better if you write it from the right side and then the left to make sure that you're actually Doing this correctly because it's harder under because comparators are weird in that what you think that is like a min heave is Actually different from axe heaps stuff like that. So I like to do it from the right side first. So if right dot Second is greater than oh is less Because we want the we wanted to be in alphabetical order from lowest to highest For the letters. Yeah, so we're gonna sort the letters in alphabetical order from lowest to highest Right that second lesson then left dot second. Okay Otherwise we want the Higher count to go first, right? So whatever is a higher count, we're gonna go make that go first. So we're gonna do right dot The higher count is gonna be first red dot first is greater than left dot first So that means that if I have a number of occurrences to and B is number occurrences one It's gonna put a two first and then be one Next value and then all those that do that thing. Let's submit. I hope this is right Compile error something something happened a compile error invalid pair char Was invalid pair into char which one Which is invalid? Dexie all type comparator. It's right Let's see. Let's see if my comparator They're right it correctly compare Okay, I'm going to do is the problem here I'm gonna call it compare. Maybe it's something has to do with the naming. It could be it I don't know. Oh, it's oh wait. No, it's I think it's No, wait, that's not it Still compiler. What is the problem argument to is invalid Uh Dexiel type Deck. I'm pretty sure it's that's right. Dexiel type compare That's right A priority queue Compare, that's right I don't know what's the problem. You know, am I writing a fear wrong auto compare left bracket right bracket Auto compare is equal to left bracket right bracket pair Left right pair left right. Okay left off first is equal to right. Yeah, should be right What is the syntax error? priority queue pair into char priority queue a vector pair and Oh, oh, I had an extra brace. That's why our extra bracket less than time Um, okay, so another priority queue is not cleared in the scope. What do you mean? I named I spelled it wrong. I need to do this one also Uh, and it got accepted. Yeah, so that's how you do this problem Uh The tricky part is the comparator You should write it based on the right right first and the left The reason for that is that will actually help you keep track of which one's alphabetical order Uh, and which one isn't so like if left if right is less than left That means that it's a min. Uh, the smallest value is going to go first if right is greater than left. That means the The uh, larger value is going to go first. But yeah So, yeah, that's how you do this question. I got a hundred percent Speed uh rate com subscribe. I'll check you guys later. Peace