 What's up guys, my name is Michael and welcome to my YouTube channel today We're gonna do another leco challenge called shorted short test distance to a character You're given a string s and a character c you got a return an array of integers representing the shortest distance from the character C in the string so in this case we have input s is equal to lovely code c is equal to the Character e Okay, and then it's gonna output three two one zero one zero zero one two two one zero the reason why I Does this is because the shortest distance between this e right here is one from from L. It's one two Three right, so that's three from O. It's one two Right from V. It's one and that's to e from e it's just zero From L. It's actually just one So it's one Because because it could go this way one e l or e l this way e is zero e is zero because it's zero T is gonna be one because good like they're one From T to e is one. So that's why it's one here It's two here because from C Is gonna be two right here one two This distance is two so that's that's two From all the shortest distance to e is one two Because the e on the right and that's two D is one and that is zero, okay? So I actually my code is too crazy here. You won't understand what I was doing, but They're so what I'm gonna do is I'm going to Give you the solution of what they did And explain how that works So first of all you want to return the An array of integers representing the shortest distance, right? So you need to create an array that has the same size as a string So that's why I did here vector int shortest distance s dot size Then oh, yeah, you got to return it. So I return shortest distance and I'm gonna create a for loop and This is gonna go through every single character Okay So few things you got to know about this In order to there are two ways you could go about this you have to go through the left left side Or the right side this way from this way Or this way because the distance that you could travel For the shortest distance could be either way Because in this case as you can see in this e the e value here is zero, right? But the shortest distance between d e is one right Actually, that's a bad example. Um, let's see. Let's say c c right from From c to the left the e value would be two From c to the right to the e to the right the value would be three So the shortest distance would actually be the minimum of Whatever comes from left and the right as you see here the this e is two and Shortest distance from c to this the right e is three. So if the minimum from the left would be two and the Compared to the one for three on the right So, how would you do this? Well first you have to go through the left from left to Your value and then from the right to your value, okay? All right, so I'm gonna just explain the solution because it's much easier if you go from left to right You could keep track of the previous Index of the last character c so once you get the value of c you keep track of that The distance it would be would be i minus previous Previous being the index of the last character c Reason why this is because in this case, let's say the let's say I find e Okay, let's say I find e and It's this index is zero one two three. So this ease index here is three So the shortest distance in this case would actually be From my current i zero minus three It would be three so it would be i minus the previous value so Zero three minus zero would be three so that'd be there in this case From two it would be Your Tuesday next is one right I always from oh is one and The index of the e is three. Remember zero one two three. So three minus one would get you to here And it's the same thing for v v's index of zero one two two E minus three minus two would be one so that'd be one. So yeah, that's the Whole just a bit from the left side. Whoops my bad. I Clicked on a click back by accident, but that's how you go from the left side. It would be i minus your previous So I'll code that right now Actually, what we have to do is we have to create Previous is going to equal in this case. They did the total integer dot min because you're trying to find the whatever value it is so I'm not gonna do that. I'm gonna do actually I could do that In a juridot min Actually, let me see c++ integer smallest integer integer Int underscore min. Okay, so int underscore min Okay, so this is gonna be the smallest integer Previous we're gonna check if s is at i is equal to the value c We said previous is going to equal the value i Okay, and then our answer in our array shortest distance at i is going to equal to i minus previous Okay, so in this case zero minus So in the beginning this these values would be actually infinity or minimum at the smallest value if possible like negative So these in the beginning that these values would actually be junk Because i would be zero minus previous which is nothing currently. You don't know what previous is. So yeah, that would be Negative infinity. This would be negative infinity that'll be negative 50 and that'll be negative 50. Okay, so that's that Now we got to go from the right side and the right side would be previous minus i The reason why we're going to do that Here they're gonna set the This says in max. Yeah, I don't think it really matters. Oh, yeah They said previous equal to in max because now we want to find the maximum value the maximum previous So that's why they did in max. So I'm gonna go from now. I'm gonna go from the right side, which is for this distance that size I Is greater than or equal to zero? I minus minus This we're gonna start at size minus one Okay If if the character at the current index is equal to see we're going to set previous is going to equal to I okay From the right side, it's going to be previous minus i and I'll explain that right now in this case Let's say We're going from the right side and our e is here If you want to go from the right side the current index from Okay, let's say let's say oh the The previous value would be e Right, which would be the length of your a which would be one two three four five six seven eight nine ten It would be twelve When we our current o would be one two three four five six seven eight nine ten Right so our the value that we would get would be Previous, which is twelve minus I which is Previous minus I twelve minus ten is gonna be two and I'll get you right here. Okay? So that's that's how you do it, but then also we want to get the minimum distance remember We need we want the minimum distance not just the shortest distance for the value So we are going to do shortest distance at I is going to equal the minimum of shortest distance at I and Previous minus I Okay, and that's your get you it Previous minus I would get you the smallest value on get you the value distance for the right side Shortest distance is our the current shortest distance is I minus previous and yeah, that would get you your thing We'll run the code shortest distance not declared in scope. I did oh shortest this This whoops, okay now we're running The integer overflow you can measure okay, so what they did was they divided by two just to get Whatever value it is To make sure there's no overflow in this case. It really doesn't matter in the beginning. It's gonna be negative and Here it's gonna change. So there we get these values submit it and it got accepted. So yeah, that's how you do this I'm gonna what I'm gonna do is I'm going to display shortest distance Every time just to show you guys what it is and Here I'm gonna display a shortest distance here. I'm gonna just see out And and then here I'm gonna do see our shortest distance no terminating string Oh, whoops unresolved overflow whoops. Oh my gosh, okay now try it Okay, so in the beginning it's going to be infinity infinity infinity then when you finally get to the character at See this one it's going to change to zero one zero one one zero stuff like that So in the beginning The love value would be very large once you get to the first e would change and then it would set the right values from here After that once you do the comparison, it would be the right values in the end. So yeah, so that's how you do this Hope you guys enjoyed this video. I'm really tired right now. I'm gonna do some other stuff I have some homework. I gotta do ready to come subscribe. I'll check you guys in the next video I might actually I'll see if I could post daily But I'll try all right very calm scribe check guys later. Peace