 What's up guys my name is Michael Lynn welcome to my youtube channel today You know what we're gonna do. We're gonna do another leak code grind challenge Basically, I'm gonna do the first 30 because I already did the first 30 questions and I'm gonna quickly You know grind these out with you guys and then I'll actually start doing them Doing them with you guys see how long it would take me to do them like actually do that because Right now currently I've already finished these Questions and I'm just going over the solutions with you guys But in the next videos After the 30 video the 30 problems. I already have done. I will actually do it with you guys All right, I'll do it with you guys I'll have we'll never have seen the problem before and I'll be I'll do the questions with you guys well for now Well for these questions, I'll just go over the solution with you guys Anyway, we're gonna do unique morse code words and what is this? well Basically, this question is is that there's a standard coding Which each letter is mapped to a series of dots and dashes So a maps to dot dash b maps to dot uh dash dot dot c maps to dash dot dash dot dash Okay, so for convenience the full table of the English alphabet is given below. Okay, so all these represents a b c d E f g h i j k Up to all the way to z. Okay Now given a list of words each word can be written as a concatenation of the morse code of each letter for example cba can be written as Cba which is a concatenation of c b and a okay, so what they're doing is They're changing whatever lettery we're at where I at Based on this morse code whatever c represents. They're going to take that Get the letter c which is uh in this case. It's a b c which is these values Right, which is this and then they're concatenating it with uh b and then a That's what they're doing. They're changing the letters and concatenating to whatever Letter it is. Okay, whatever uh morse code it is. Okay. We'll call this concatenation Transverse of word return the number of different transformations among all words we could have Okay, so Given the input words gin zen gig msg The transformation of each word is gin becomes this zen becomes this gig becomes this msg becomes this There are two different transformations Dash dash dot dot dash dot and dash dash dot dot dot dash dash. Okay, so The reason why they're saying two different transformations is because these this transformation is the same thing as this transformation Right, so we're only counting different ones So And this transformation is the same thing as this transformation. So that's why In total there's only this transformation and this transformation As you can see here there's only this transformation and this transformation because we're only getting The different transformations. Okay, so how are we going to do this? Well I will basically You guys have to I'll give you time to do this do this on your own And then come back to this video and I'll go over the solution All right guys. So now that you've tried to do this yourself I will try and I'll explain a solution to you guys. So first of all, what do we have to do? Before we think about anything We got to think about the step by step process of how you would do this first of all We're giving a list of words, right and we're have to convert through these words these words Into a transformation That's what we have to do number one. All right, so I'm going to put a comment right now convert Each word into a transformation This transformation represents the source code right not source code the um the morse code and once we convert this We need to get Only the different transformations Now we have to get only the different Transformations And then after that we return the total number of different transformations Okay, so you guys understand this do you understand this algorithm currently? We basically have a list of words We need to get We need to convert each word into a transformation like in this case. We have each word, right? So this is a word we're going to convert into its transformation We get only the different transformations. So Uh This this this So there's only two different transformations and after that we return the total number of different transformations, which is two Okay, so this is basically the algorithm All right now. How do we do this? well First I have to go through every single word in the list So here i'm going to in order to convert each word into a transformation I have to go through every word in the list. So i'm going to go through i less than zero i less than words dot size i plus plus This loop will get go through every word in the list. So jin zen jigig msg Okay, so words at i this is the current current This is the current Word that i'm at words at i. Okay Now I need to Once I get this word word at i right I have to convert this I need to convert this to its equivalent transformation Right, so now this I need to convert each word at i to this equivalent transformation. How do I do that? Well, I'm given this table right this full table of 26 letter english alphabets I need to use this table. So I'm going to Do a string um, let's see How should I call this? Okay, my bad. Okay. Um vector Actually it wouldn't matter string um Table whatever as 26 And I'm going to paste this All these 26 values right here. Okay. Uh, let's see I think this is the equivalent to do it. I'm not sure if these brackets really matter, but So this is the equivalent table all 26. Okay So now What am I supposed to do? I need to convert Each word into a transformation. Well, what is this? This is a each word. How do I convert each word to its transformation? I have to go through every character in each word. How do I do that? You need to use a specific Specific for loop. So I'm going to go up and search online. I forgot how it works For each character in a string c plus plus Sometimes sometimes people forget, you know, I forget sometimes Okay, so this is how you would do it for each character in the string do something with the character. So for for Uh, let's see. What was it for Character and percent for each character Uh, let's see see in the words list We're going to do something with the character so now What am I going to do with this character? I want to convert this character to its equivalent Morse Okay That's what I want to do And then I got to concatenate it with With the next character and the next one and the next one So I'm going to have a string called to return or transform. Let's call it transform Transformed string. Okay That's going to be empty For every character I'm going to transform each character to its equivalent value in this table Once I transform it, I'm going to concatenate it to my transform string. So this would guarantee a transformation on whatever Equivalent character to its equivalent Morse. This will guarantee it So I'm going to do transformed string Plus equals what am I going to transform it to I have to take the character and transform it to its equivalent value So The current character is C. I want to transform this Oh, not see the current. Oh, yeah, the current. Uh, maybe I said let's not call it C. Let's call it current character Current character current see kerr char. Yeah kerr char kerr char. All right kerr char is current character Okay, I want to convert its this current character into its equivalent Uh, 26 value. So I'm going to go to the ASCII table So here's the ASCII table and we're going to look up What how to convert the current character into whatever its equivalent form is so Here's the ASCII table A A's value is 97 B's value is 98 C's value is 99. Okay So how am I supposed to convert this A to its equivalent value Of 20 of 26 characters, right? ABCDE to this current value. Well, we're going to subtract Whatever my character by 20 97 we're going to subtract by 97 and that will change this index to become zero That way if A is A is going to refer to zero B is going to refer to one C is going to refer to two D is going to refer to three, right? So then A is going to A's value is going to be A's value, which is like a zero Is going to be this Right zero is going to be this B's value is going to be this because it will be one C will be this And D will be this and so on So I'm going to subtract My current value A was 97 by 97 to get back to zero to go to zero because I want to index it by zero I want to index Each of these by zero, right in order to get its equivalent value into this array of 26 So I'm going to take current character minus 97 Okay, and I am in order to have to convert it to an integer first Because I mean that's just how it works in C plus plus I'm going to convert the character okay Once I convert this into an integer I need to This is will give me the current index of the Whatever value I'm changing it to and so I need to get the equivalent based on that index Which is table so in order to have to do table At the index of this I hope you guys understand what i'm doing Okay, I basically changed this its equivalent character into its equivalent form by subtracting 97 and basically that changes the index when i'm indexing to back to zero Right, and that's the reason why i'm doing this so now now once I get my current index And its equivalent form in the table I could index it at that table So then this would be a's value would be this b's value would be this C's value would be this d's value to be this and so on and so forth So this would get each character's equivalent morse Then I concatenate it into my transform string and that basically will This would basically return all the different transformations for each character, okay So just to Not below my brains out. I'm going to print this And I'm going to run the code so Ideally I should get oh what what's going on uh Oh, I forgot a semi colon Okay, uh wait. No, I need a semi colon. I Okay Oh my gosh, okay, uh, I ran the code too much too too many times, but Uh, okay, so ideally this is what it will do. Okay, um I ran the code too many times. Jeez, okay, uh So ideally this this would return its equivalent Each of its equivalent transformations. Okay. Now. What do I have to do? Now I want to get each different transformation I need to get only the different transformations and return the total number of different transformations So which data structure prevents duplicates? duplicates The date which data structure prevents duplicates in C++ well if you remember it's the set set remember set prevents duplicates Mo let's just set is a data structure that prevents duplicates If I have a set of things and I add something new it prevents duplicates from occurring So what am I going to do? I am going to have a set Called different transformations Okay, then for each transformation And here I'm going to add it to different transformations. So different trans Uh, whoops Dot insert we're going to insert each transformed string And what is this going to do? This is basically going to Prevent duplicates from occurring after that This would get only the different transformations and it prevents duplicates after that I want to return the total number of different transformations. And how do I do that? I have to return different Transformations dot size That will return the total number of different transformations Okay, and that's basically how you would do it as basically the solution Let me submit it Wrong answer. What did I do wrong? Uh, let's see. What did I do wrong? Something must have occurred Okay, there is some some some issue. Hold up Mm-hmm What was the wrong answer? Output was three expected two What was the different transformations? Insert one insert two it's a three Let's see. Let me see. Let me see this solution that I had before pretty sure. I did exactly the same thing though Yeah, yeah Yeah dot size I mean I didn't have 26 but that wouldn't matter Convert this cast this That's the only difference Let's see Let me submit it That's really strange Okay, I am going to debug this And see what's the issue of why I am not getting the same answer as what I had before Let's see to add. Okay. So this was my solution before And let me just see which which did I forget something? For every character plus equal I said that return I said that size Plus equal for every current character Let's see. What am I doing wrong? Okay to transform insert transformed string Transform string empty table Let's print out. Let's print out each transformed string. Why am I getting Let's see. Why am I getting the different answer? This is a good thing about debugging debugging helps you Debugging helps you doing these problems They actually help you do these problems. Okay, so I have dot dot dot dot dot dot dot. Oh Have an equivalent space What is the why do I have a space? That's a big question. Let's see plus equals It is empty though It is empty Convert it into integer I don't think that would matter here current character convert it to its integer plus minus 97 that should work table integer Oh Could it be this? actual parentheses I don't think I would have mattered Hmm Something's not doing the way I thought it would be Okay, guys This calls for drastic measures. We're going to open visual studio and we're going to debug this code with you guys You know Sometimes this is why I love about these videos that sometimes that sometimes things don't work out the way you think it would work out and you actually end up learning way more about about Code that you never thought it would happen before but yeah I'm going to debug this and see what's the issue why My previous code worked and why my current code is not working Let's just see what what's going on. Okay, so I'm going to what I'm going to do is okay. Here's if here's my Here's my code. My code forces code I'm going to paste my solution here and I'm going to call the function passing in those those The two values there and then I'm going to debug it. Okay I'll debug the code and see what's going on Okay, so Here's a current program that I created Okay And now we're we're gonna let's remove this this solution. Let's remove this Keep this we're gonna keep this. Oh it formatted correctly for me. Okay I'm going to create a vector of words Is it in? Oh string. Yeah string We're going to push back What are we pushing back gen gen zen gig msg? Okay uh see Gen zen z msg just push that's let's do let's do it this way. Okay Gin zen pushback gin pushback zen pushback gig This is just to make sure that They're in the right order I mean, I could have done like a one initialization there But I didn't want to okay now we're going to do see out a unique representation And we're going to pass in words and yeah This passport reference. Okay. Yeah this passport reference. Okay Let's run this Let's see why I'm not getting the right answer. Okay So it's going to start out with the transform string Okay, we're going to add Okay, so This is right. This is right It was only until the last one. It was weird. So let's go to this do this and We're gonna do this until we reach the last one, which was this one Uh, which eyes this eyes two sides was four, right? It was msg. That was weird Oh, I skipped it. Okay. My bad. Uh, okay. Let me just quickly do this again All right, one Two Three. Okay. Okay. So sides is for Three msg. Okay. So we have transform string Change the m 109 should minus 109. What is 109 minus 97 should be Dot dot space. Oh, I'm having an issue dot dot space. Oh I didn't copy and paste this correctly Uh, see that's the issue guys This is wild This is this is a good thing about programming that you learn things that you didn't expect you would know So I didn't copy and paste the uh one of the Where is it m a b c d e f g h i j k l m this one Is a way too many spaces So, okay, so this is This is m. Let's see a a b c d e f g h i j k l m Dot so this is what should be And I didn't I didn't copy that Uh, there should be no spaces here. I think yeah, there should be no spaces here All right now it should work when I run it Yep to submit it And it gets accepted. All right, so that's basically how you do unique morse code words. Sorry you guys. Sorry guys If uh, if if the video turned out way longer than I thought but it's good to learn debugging It's good to learn these set of skills right come subscribe