 What's up guys, my name is Michael and welcome to my YouTube channel today. We're gonna go over group anagrams So you're given an array of strings. You got to group the anagrams together. Okay, so the anagrams are basically just Same strings that have like the same Frequency of characters. So like you see this eight eat and tea they're anagrams because the frequency of characters like it has one a has one t has one e and All these have the same frequency of characters. So that's the reason why they're grouped together in a new array This one Nat has the same frequency of characters as tan and then bat has this is no No other frequency of characters as other ones. So if they just added it as another array So how do we group these together? I tried really hard and I still got TLE Like I tried counting every single frequency of characters between each each string and then like comparing Each of them using a map and then I try checking if the number of occurrences are the same And if they're not then then I'll push back into a new array. Yeah, this actually is not a good idea to do So now I'm going to actually just I'm going to explain the right solution on how to do this and Hopefully you guys you'll understand so Actually, it took a while for me and I not took a while for me Basically, how do I I had to look at another person's solution and I understand it now so first let's think about what an anagram is anagram is where Frequency of characters are the same, right? But technically you don't have to How what do you say this you don't have to calculate the frequency characters every single one like that's what I try doing What you could do is you actually could just sort each character Like each each string then if they have the same permutation and when I say permutation like same ordering, right? They guys sort eat. It's gonna have a T. I'm not a T a E T, right? If I sort all these by alphabetical order for each characters, so they'll be a E T eat will have be have a E T also and T will have a E T also So then that means that they're if you sort them all they're gonna have the same the exact same The exact same String right if you sort them all by alphabetical order, so that's what we're gonna do So first we're gonna well. I have to create a to return Vector first because we have to return to a turn but we could create a map and then we're gonna store every string with it's its own Vector for each string right and this that's what we're gonna do. We're gonna store each Same permutation not permutation. Yeah, same same ordering in a map of this. So this is what we'll call it order, right? What am I gonna do? I'm gonna go through every single string in my List of strings so each string And what am I gonna do so I'm actually going to sort each each Yeah, I'm gonna sort each I'm gonna create it. I'm gonna create a separate another string called a s2 and I'm going to sort each string So s2 is gonna equal to source So I'm gonna sort each string. Wow, that is a mouthful each string So I'm gonna sort the beginning and the end by alphabetical order And I'll be my s2 right and then what I'm gonna do is I'm going to store this sorted order into a my Map of my hashes of map my order right so I'm going to store this in the hash map as to and what this is gonna do is it's going to Make sure that I only add the ones that have the same order. So look, so let's say I have eat tea and Where's all the one eight right if I sort them all have sort them all by alphabetical order They'll all have a E T right a E T and then I'm going to basically Store that my map is gonna have I'm gonna map a E T with a vector of strings. I have the same Order as a E T right so then that's what I'm gonna do. So I'm gonna sort First I'm gonna sort the string then I'm gonna store order is dot push back each string So once I sort this string, right, it's gonna have the same order all the ones that I'm gonna go through all They raise right and then I'm gonna it's gonna have an order I'm gonna store that order into my map called order and I'm gonna push back that string So then that means that each string that has the same order Is it gonna be in a the same vector like the same? Same same vector array. That's what it's gonna be. So yeah, that's what I'm gonna do here And then after that I'm gonna go through my map again and I'm just going to push back It into to return. So I'm gonna do for every pair in my vector in my Not vector in my order in my order pair, right? So remember we or we mapped every single order like the alphabetical order sorted towards it's Corresponding vector that we pushed back, right the each pair that we added Each string that has like so what we did was we grouped every single string that had the same alphabetical ordering Right together in our to map in our order map So then what I'm going to do is I'm gonna go through every single pair in my order And what am I going to do is I'm going to add just add the pushback the pairs second and pairs second is just the each strings a vector order, right? So the each Array that we added right. So yeah, that's what I'm did here. So I think this works So I'm gonna explain it again in case if you guys don't get it. Okay. What's the problem here sort? Okay, so I can't I can't you said equal to it, but I can do a So after any sort Okay, so I'm gonna create a separate string called string s2. It's gonna equal to each string And I'm gonna sort string s2 So I don't have to deal with this Because they say I can't return the sorted order. That's really weird though Okay, let's run the code Okay, I think this is right each t8 bat that's not Yeah, that's a mitt. Oh, and I got accepted. Okay. So yeah, that's how you do this question So I'm gonna explain it again in case if you guys if I kept mumbling over and over again So remember anagrams have the same are the ones that have the same frequency of characters, right? So each t and eight eat T and eight have the same frequency of characters like the number of a's are the same number of e's are the same The number t's are the same right? They all have one when a1 e1 t So if I were to sort each of these Anagrams with the same anagram they're gonna have the same alphabetical order So they're gonna have a e t. All right, they're gonna have a e t. So what I'm gonna do is I'm gonna store I'm gonna maintain a ordering of each alphabetical ordering to have an array of Vector array. That's what I'm gonna do. That's why I did here map so my I'm gonna map every single alphabetical alphabetical ordering to a vector array of vector string right, okay So what did I do? I went through we're gonna go through every single string in our vector string and we are going to Sort each of these values Right, we're gonna sort each of these values to it by alphabet alphabetical order Okay, so then we're gonna have an ordering alphabetical order, right? Then what we're gonna do is we're gonna store that Using the map and push whatever the ones that have the same order with the same Alphabetical ordering, right? So the so the ones that have the same order as you can see here It's gonna store the we're gonna push back the ones that have the same order in the in the with the same Alphabetical ordering pair, right? So like if so here in this case e to t Eight, right? We'll have all to have the same alphabetical order. So it'll be a e t So then what would what we did was we pushed a e t It's gonna have the values of eat T and then eight, right? Because they all have the same value ordering and then oh keep going and keep going, right? So and then like Nat and Tan They have the same alphabetical ordering pairs and then we paired the A I don't know ABCDFGHLKLMOPQRSTO Yeah, it'll be a and t right So Nat will have a and t Tan will also have a and t so the ones that have the same alphabetical ordering pair Order with the same ordering pair as to We're gonna push back into that array. So the ones that have the same ordering pair We're gonna push back into that that new array if it doesn't it'll push back to a new array That's what it does like if and if this was a new ordering pair. I'll push back to a new new one So yeah, that's what this code does it goes through every single string It sorts For each string it's gonna sort the By alphabetical order and then what I'm gonna what it does is it takes that alphabetical ordering, right? And then it adds the string Towards that alphabetical order, right? So like a ut would have that order a order a ut is going to have Whatever string that I passed in through every single one value. So yeah, that's how you do this question I hope I hope I explained it correctly. I really hope I explained it correctly. All right ray com subscribe I'll check you guys later. Peace