 What's up guys? Mike the coder here today. We're gonna go over the next problem set of a palindrome reorder So this this was a problem that I actually skipped, but then I'm gonna come back to it and now I'm back to it Yeah, so yeah, let's actually go over this so given a string your task is to reorder its letters in such a way that it Becomes a palindrome. So what does that mean? That means that? Essentially is that a palindrome is a string that contains the original string forwards and backwards Okay, so our first input example was a a a a see a See a cb's a okay, so you have a a a a See a cba. Okay, and then what they did was they? Essentially what they did was they reordered it in a way so that now it's backwards Forward and backwards so it's now a palindrome. So as you could see this Sees their output in your final output is this is this This string and if we were to I'm gonna show you guys in paint real quick I'll show you guys in paint real quick to show you guys what I mean by this, but if we were to If we were to If we were to Type here. Yeah, okay So if we were to take the new string here and we try to check if it's a palindrome So as you can see here the first letter is equal to the last letter, right? And then all these letters are this the second letter is equal to the second-last letter, right? and all these letters are a palindrome because second letter is also a mirror image of the third letter, right? The third letter is also same and the fourth letter is also the same and Then the final letter of B is also the same. So essentially what we're trying to do is we're trying to reorder the original string a a a a see a Cba to an actual palindrome and if there is no solution as a no solution We print out no solution because that means that it's actually there's no solution for it, right? because then Essentially, there's a it cannot be reordered in their palindrome. So I'm gonna show you what I did Just to show you the code why I did essentially is this is not that hard of a problem what I did was basically I just I Kept track of each of the character and then how of the occurrences of each character So like if you don't have to do what I did, but this is this is my solution. So so here I had the original string of a So let's look at the original string not the output not this output But the original string right and what I did was I just kept track of every character the occurrence of every character. So a has This a right this a has Three one two three four five six has six six of these right six of these I'm gonna actually just type I'm gonna type. Okay, so a has about six has six Yeah, I'm just gonna type actually this this takes too much time. Yeah, so a has about six B has Let's see how many how many values as we have has one right and then C has two C has two right. So here we have a has one two three four five One two three four five six. Yeah, he has one and this he has two So what I did was to essentially build my new palindrome is that I just kept track of each character from adding each character to the Two strings, right? I have two strings and I just kept track of adding each character to the front and the back Right and then as we keep going that we're gonna just decrease the number of occurrences of each character Until we use all the characters in the string and then we could just check if it's a palindrome In that end, right? So here if I like if I add First character there's six, right? I'm gonna have six I have a Adding into the front then I'm gonna have like a also add into the back, right? So the six would be decremented to like four. So this would become four, right? Because I used up two characters, right? I use up two and then I could have like C I'm gonna add C to the front also and then I'm gonna have add C to the back Of a right C C and then a here, right? And then so on and so forth, right? And then this C basically gets decremented to To zero C becomes zero and Yeah, we just do the same thing over and over until we get to For like all the even occurrences, we just do the same thing until we until they're all used up the last auto occurrences I just put that in the middle, right? So now this four gets decremented So let's add all the values of a four. So let's do that Yeah, so we could do like a we could do a C a dot dot dot dot dot dot a C a right? So that's afford in the back and then this a becomes decremented again by two right because we added at the front and the back So now this becomes two and then we'll do this again. So let's take this two out and then do it again a C a a dot dot dot dot dot a a C a right and then yeah, so then this two becomes zero and Then the last one is just going to be B Right, so let's decrement this last odd value to be remove it Move the current to the B and then we add that into our final value in the middle a C a a B a a C a, okay? a C a B a a C a a C a a B a Yeah, okay, so then in the end This is going to be a palindrome if you reconstruct it and If you can't do this like it just doesn't work You could also just recheck if it's a palindrome in the end because that's just going to take a One one loop to go through it and check if it's a palindrome again if it's not then yeah, then it's definitely no solution So yeah, that's basically my solution on how I coded this up and I could show you the code now because uh, yeah So yeah I'll show you the code So first here the first part is the solved I pass in the string so okay I'll start in main so here I read the string s and then I do see an s that I call solved Okay, so solve is going to just do all the reordering of the string. Okay, so here um I Create a variable of n is just equal to the size of the string because I don't know I don't I'm just too lazy to type in s dot size over and over again. Okay So here my map O represents the occurrences of each character So I'm going to map the character with the number of occurrences of each character And that's basically what this frequency thing is doing I'm just keeping a track of the frequency of each time a character shows up So this builds a 6 then B 1 then C 2 so that's what this does. Okay, so that's what this for loop does String W is just like the the the front string right the front string that I'm building So that this is the front string of a right? Whatever in this value in the in a so a then a c then a c a right? That's the front string that I'm building. That's what this w is so here Here what I'm doing is I'll go through every single Character in a we actually not character Yeah, every single occurrences in the in my occurrences list for character and integer right in our map And I check is is the occurrence odd. Okay, so if it's the occurrence of odd, then I'm gonna put that first So for some reason I just did odd first You don't have to it really doesn't matter if you did it do odd or even or whatever But yeah, the string s string W actually represents them the middle one then I Think I coded this really strangely. Yeah String string W string W. I might not ask string W represents the string in the middle So that actually represents the My bad it represents B, right? So I have a I have a string B String W that that is the middle value of the string that has only the odd the odd occurrences I put that in there. Okay, that's what I have in the middle. Okay, I Know string begin is the one where we're talking about of actually Actually having an adding their currencies for each of these so the string begin is a and then AC and AC a That's what string begin is so I'm building up like building up the the string in the front Okay, so for that I loop through all the occurrences of my map X and O represents the occurrences of all the character Then here what I'm doing is I'm just going to loop through to the middle size I'm not I'm a Yeah, what I'm basically doing is I'm just going to loop through through half of it. So like if If I'm going through like one two three or five six, right? I have six here I'm just gonna add three a's in the front, right? Because if you build the string in the front you could add like If you have six, right? You could add like three a's in the front and then three three B Three a's in the back also, right? Like as you could see here in our string occurrences, right? Building our string if it's even we could just add That half of it in the front and then half of it in the back because you could see you're like a AC a AC a right AC a So here are a's we had three a's in the front and we had three a's in the back So essentially what we could do is we could just have Loop through half of it half of the occurrences and add those that number of characters in the front Okay, so here essentially what this is doing is that it's going to go through the The map and just add half of the occurrences half of the occurrences for the even values and then Yeah, so then begin plus equal to x dot first, which is going to be the the character the character of our of each of the care of the values in the map and then I'm just going to Decrement the number of occurrences at that character. So that occurrences at x dot first gets decremented So then each time Yeah, each time when we go through it we're just going to add half of the Letters for each of these first in the beginning and then half of the rest in the end So that's for the beginning right for the beginning. Okay, and in the end We do the exact same thing for the string end We do the exact same thing as what we did here in the beginning What we're doing is just we're just going to take half of the letters of the occurrences and the remaining characters of the occurrences so here we have a Remember I had three a's in the beginning right and then I think I had three a's and then I had two Three a's in the beginning three a's in the end and then I put a Since C is equal to two there will be one C in the beginning and one C in the end Right, so here as we could see here We had three a's in the beginning and one C and then here we have three a's in the end and one C So that's what this does is that this end does exact same thing as beginning But what it's doing is it's just going to build off of that Build off of the remaining of characters occurring into that or even Who's the number of occurrences are even and build them off and and add them into the our end string Okay, so now that we added all the things in the end string I reversed it and the reason why is because I should have done on end Equals x dot first plus and like adding into the front But instead I added into the back and because of that I just reversed it because I was too lazy to add into the front But if you want to it's better to just like append things to the front so that they're actually like mirrored together But yeah, and then in the end. Yeah, I just reversed it So then so then now they're actually like mirrored together, right? So we would have a ACAA in the front and a ACA that's mirrored who's also reversed in the back Then what I do is I build a new string called results with the beginning then the middle and then the end so the beginning is going to have like The string of a three a's and one C and then the end is gonna have three a's and one C You're also right and then the middle is just gonna have one B. It's just gonna be all the Characters that had odd values all the characters had odd values You're gonna put that in the middle and then they'll be your answer Okay, and then what we do is we check if it's a palindrome So if it's a palindrome then we just print out our result Otherwise we print out no solution and that's the basically the gist of the problem. I hope you guys enjoyed this video I had to make this video pretty quick rate com subscribe. I'll check you guys later. Peace