 What's up guys? Mine is Michael and welcome to my YouTube channel, and you know what? I'm doing something a little different I know I haven't been uploading videos for a year actually months But because but I'm back at it again, and I'm gonna do something different. I had a comment the other day saying I'm just basically reading the same old Book over and over again and not actually implementing the algorithm So you know what? I will actually implement the algorithms from now on. I'm gonna do that What better way to do that is actually to do some algorithm problems. So Here I have already done the first 30 easy problems and leak code and What I'm gonna do is I'm basically going to do them with you and Do all the easy and then maybe you do all the hard I'm not hard. I'll do all the easy do all the medium and then do all the hard and then once we do that We could you should be pretty decent algorithms if you could solve at least a thousand a thousand one hundred thirty six Problems on the code. You should be able to do very well in algorithms and in most of your Most of your algorithm interviews at least if they interview ask you an algorithm pressure should be able to do it Yeah, so today what we're gonna do is What I did done so far is I clicked on Problems on the code so create an account on the code. It's a free you use it's free. Yeah, it's free. You could use your Gmail account you can use your Facebook account. Whatever it is create an account on Lee code and click on problems Why did and then you click on algorithms? Okay now Generally What I would do is I would then I would then click on difficulty So I sort it by difficulty and then I start doing these so that's one thing you do So today what we're gonna do is we're gonna do the first question called defanging an IP address. I Already done this so I'm cool. Please you guys don't see the solution. Okay. Don't do this. Okay Given a valid IV IPv4 address return a defang version of that IP address so defanged IP address replace every period dot with a Bracket dot Brack other bracket. So let's see the example Okay, an address is one dot one dot one dot one and then the output is one Bracket dot right bracket one that bracket dot right bracket So it replaced all these dots with a actual brackets the actual brackets And the dot. Yeah, so that's what they did You could do you could do this question in whatever language you want They have a Java Python Python 3c sharp JavaScript Ruby. I'll be doing it C++ Just because this channel C++ right. I mean might as well do it. Okay, so we want to replace every dot with a bracket bracket First thing to do we're given the string right first. I'm gonna create a string called to return This string is just an empty string What am I good? What am I gonna do? Well, I have to go through the whole whole string right every time I see a dot I want to replace it with a bracket Bracket. So what way to do that is it with a for loop So I'm gonna go through for loop in I equals zero. I lessen address dot size. I plus plus This is gonna go through the Whole string one I won every single character in the string until I reach the end of the string The size of the string I'm gonna keep going until I reach the end of the size of string basic for loop. What am I gonna do? I'm gonna check if the character The current address current index at I so what I'm gonna go through this whole string if the character at the current index at I at I so Ever care to this is so it's gonna start one It goes like this and goes that goes that that that's what this for loop is gonna do if the address at I is if the Current index I is equal to a dot What are we gonna do if equals the dot we got to replace it? All right, so to return plus equal place it with Brackets dot otherwise Otherwise just continue the Just add whatever character it was at that location then after this world we're gonna do return to return Let's run the code to see if it passes their results. Whoops. It doesn't oh, I forgot equal equal. You got to remember that Okay, another compiler. Oh, oh, okay. Whoops my fault characters have to be Single quotes single quotes characters are single quotes That's how it works Run code. Let's see. Let's see what happens All right, the output is exactly like they expected then we click submit This is my previous I already started to submit it and would you look at that accepted? You're faster than 54% of C++, but the memory usage is less than a hundred percent of C++ all my solutions So that's this is my solution. It is faster than 54%. Let's see other now now that we have this running so far Let's see other people solutions Okay Let's see this guy's solution. Okay, so what he's doing is whoa, this is really really something. Okay, um So he's doing the same thing He is creating a new string with the seven spaces This char point it puts a new string with seven more spaces than the size of the the original string and He's gonna loop through the previous string I mean the current string the current string of address. He's gonna loop through that and he checks if this is a dot then he He basically Sets whatever it the he sets whatever His new string temp string to be whatever the left brace Then he increments the index of what it is that and he does he sets the character Whenever not characters and if we know it's a dot, right? So he sets this after that then if it is equal to dot He sets the right part. So this sets the right part of the The string so because we have a left rack dot right rack. We're replacing every dot with the left rack Every dot with left bracket dot right bracket. That's what he's doing here And then after that he's has a null pointer No character at the end because he I don't know why he's doing this with C There's a C and yeah, that's he returns that let's see another person solution Let's see. Let's see. Let's see. Let's see. Ah, let's see. Ah, that's fine. Let's see. Yeah, you got it I haven't seen any C++ solutions. Oh Okay, so this is a good one you could use replace. So what he's doing is he's gonna Use that dot find function which finds the position of where there's a dot in his string of address Once he gets that position He's gonna loop Loop through it, right? So this checks if the position is not The end and position and position. Yeah, so this is this end position while he does that he's going to replace every character at the position with this string and then he's going to basically Find it again, he's gonna find the position again and He's going to move Whatever position he's at plus three. So what this solution is doing is Let's see if I could get paint Uprunning this solution It's funny how we could get to see like multiple people solutions, but this is what he's doing. So let's say my string was one dot one dot one This is my string. This is what he's gonna do He's gonna first find the uses these the fine function, which is gonna get him this position of the dot once he gets this position He's gonna replace this with dot Bracket Bracket, so this is gonna replace that so now Afterwards, let's see. I could just I'm gonna erase this so now afterwards After replaces that I'm gonna move this to the left Okay I'm gonna just retype read redo it this way. So after replace that it's like this, right? But now the position is still here. So now what he's gonna do is gonna increment the position by three So what does that do that moves the pointer of? Where this location is that to here, okay So then now this is gonna continue on searching through the next dot and then he does the same thing over and over again So that's the solution. I think the solution is actually much much better actually Using replace because you're actually using C++ functions. I like what I'm doing. I'm returning a new string See is there any other? Yeah, that's basically defanging an IP address I hope you guys I hope you guys actually try it out yourself before Looking through my video solution That was it for this video. I hope you guys enjoyed this video. Sorry if my My microphone is not doing that. Well, it's because I Microphone broke and that's why I need to get a new microphone. So yeah, yeah That's my solution for you come subscribe. Thanks for the 1000 subscribers. By the way, I'll check you guys later. Peace