 What's up guys, mine's Michael and welcome to my YouTube channel and today you know what we're gonna do another lead code grind and I Haven't been uploading out many videos lately mainly because my microphone died and I've been using a really bad microphone, but now I have a new microphone and this one is working pretty recently and so We're gonna do another lead code challenge. This is gonna be called remove outer parentheses. All right This is gonna be a little bit more complicated but Bear with me. I could I'll explain the solution But you guys have to try to figure it out first and then if you understand the then if you listen to the solution You'll be like, oh, this the solution makes sense so What is removed out of parentheses? Well a valid parentheses string is either Empty that means that has nothing in it or it has a parentheses and whatever value and then write most parentheses, right or A plus B where a and B are valid parentheses, right? Pleasure to mean string combination. Okay, so for example, you could have empty string left and right parentheses you could have Left left right print that parentheses right parentheses left parentheses right parentheses, right? And then here you have left parentheses left right left right left right right, okay, and Basically all these parentheses have a thing in common every time we have a left parentheses We always end it with a right so here you have a left and you're gonna have an ending here and then here We have an left here right and then we have a right here Right you guys understand and we have left parentheses here and we have a right parentheses there So each time we have left we have a right, but then they don't have to be right next to each other as you can see here this left parentheses It's right is all the way over here and this left parentheses Although it's right is right right next to it the right parentheses, right over here You have a left parentheses here and it's right for their parentheses is all the way over here So it doesn't have to be right next to each other So a valid parentheses string s is primitive if it is not empty and there does not exist a way to split it into a Plus B where a and B are not empty valid parentheses string Okay, so this is just saying that okay, it is valid You could split it into a plus B. Okay, where they're both parentheses. Oh given a valid parentheses string s can consider its primitive Decompositions s equal p1 plus p2 where p plus k are okay, so these are all valid parentheses string This is just jargon saying that they're all valid parentheses strings Okay, when guys whenever you read these statements you gotta say you gotta think to yourself How can I simplify this? Understanding cuz they're trying to confuse you in these these problems. They're mainly trying to confuse you get into your head But you can't let them otherwise you would not do right on these interview questions Okay, so you can't let that get into your head. Okay? Return s after removing the outermost parentheses to every primitive string in the primitive decomposition of s so We have to remove the outer parentheses. Okay, so here given this this string, right? They're ever moving this parentheses and this parentheses, so these are the outermost Which is why whatever here is left Inside these left right left right is corresponds to this left right left right You guys see so this corresponds to that and this out of parentheses We're moving these out of parentheses. Okay, the ones I'm selecting here this one and this one Right, so now they're gonna be gone and we're gonna have only these left this left and this right And that's why this this left and right corresponds to that To these lists left and right. Okay, so you guys have to understand. We're not just removing these parentheses Okay, we're not just removing whatever every time I see a left I'm gonna remove it or every time I see a right I'm gonna remove it or every time I don't see one remove it or not We are removing only the outer parentheses. Okay I'll show you another example right here example to okay This is a long long string right here. Okay, you we got to see what they're removing here So they're gonna remove this parentheses and this one. Okay, because these This correspond these parentheses correspond to these parentheses It's the inner parentheses and they remove the outer ones Then we have this parentheses and that parentheses and they're removing this and that Leaving only these inner this one inner parentheses and that corresponds to this Okay, then we're gonna remove The outer parentheses of this whoops my bad this one and that one Wait, let me see hold up. Hold up. Yeah, this one and that one. So that's gonna leave Yeah, this one that one that's gonna leave These parentheses and then we're gonna remove. Yeah, these parentheses, which is this one But they're only gonna remove the outer ones right so that's why they leave These parentheses this one and that one inside Which is the reason why the rest are left here the rest are left here, okay? So this just shows an example that we're only removing the outer parentheses as you can see here example three though Removing the outer parentheses. So here the reason why We're it's empty is because they're removing all the outer parentheses So as you can see here, all these are outer parentheses This this and this are out of parentheses nothing inside. So that's why it gets removed this one and this one are out of parentheses Nothing's inside so these outer parentheses get removed. That's why it returns an empty string, okay? Okay, so I hope you I'm gonna give you time a few minutes to do this yourself And then I'll come back and you have to think of a solution on how to do this, okay? There is a way to do this with one pass Okay, one pass as in one loop you could figure it out, and I hope that you do Okay, I'll give you a few minutes and you got to figure it out All right guys before I show you the solution. I got to tell you how this is gonna work So given this string we need to remove these parentheses the outer ones this left one this right the outer ones of these So this left and that right so we're gonna remove these parentheses are to do that I have to have an index to keep track of all the characters I'm going through so all these values all these values right here all these values. I got a keep track So I'm gonna call that I I is gonna go from the start to the end and keep track of Whatever I'm looking at. I need to keep track of the The values of the inner parentheses I want to add the inner values, right? I don't want the outer Right, but I want to add all the inner so I'm gonna have an index called J So here I'm gonna have it in J It's first gonna point where I is Okay All right, so I'm gonna go J. So in this case I is equal to zero J is equal to zero in the beginning Now I'm gonna be like, okay does Does the value at I Whatever I'm looking at is that a right parentheses Because if it is I don't want to add it If it's a right parentheses, I want to decrease My index of what I'm adding by one. So in this case, let's say I was over here. If this is a right parentheses I'm gonna decrease my index by one. So then I'm not gonna add it. I'm gonna go to the inside You get it? And if it's a lab parentheses, I'm going to increase my index whatever I'm adding and I will add whatever that is, right? And that's how you get the insides values. That's how you get the inside values. We're gonna keep track of The index like this. Okay. All right guys, so I'm gonna explain to you how to do this now Okay. Well first I gotta undo this Okay, so first of all, we need a string that we're returning So I'm gonna create a string called to return and this is gonna be the new string That's gonna have initially it's gonna be empty But this is gonna be the new string and initially it's going to have the It's initially it's gonna be empty and then we're basically just gonna return the values that are With the outer parentheses removed. That's what this to return is for and we're gonna return it in the end Okay, this is just to make sure that Returning it. Okay now we have to go through every value in the string s and And we have to keep track that using To keep track of it We have to go through every value in the string s and we're gonna use a simple for loop into i equals zero That's gonna be our index called i and Then we're gonna go through every Every value Until we reach the end of s dot size, which is the last the last character Now we need an index like I said j We're starting in the beginning at zero. This is gonna keep track of the Whatever value we're gonna add. That's what it's gonna do. Okay, and Basically if it is if it's a basically if if we reach a Right parentheses, we're gonna decrease j by one and if we Reach a left parentheses, we're gonna increase j by one and we're basically going to add Whatever value s at j. Okay. That's what it means. So how am I gonna do this? Okay, so first if uh S that I which is whatever we're looking at right if this is a Let's see If it's a left parentheses, right we got to check if it's a lot parentheses. How do we do that? We have to use ASCII because if whenever you go through every Every value inside your string. That's a character. So you have to go through By checking the character number or the ASCII the ASCII is basically a value For whatever character that is assigned. So I'm gonna search up ASCII right now ASCII table All right. So which one is left parentheses? Left parentheses. Let's see. It's Right here or right parentheses right parentheses 41. Okay, so if my value is 41. This is right parentheses I'll put a comment right here just to say right parentheses I'm gonna decrease j Okay now if S that I Is now a left parentheses So which one is that left parentheses? This is left parentheses. This is left parentheses. That's 40 If it's 40, I'm gonna increase j Now I'm going to check one thing if J is greater than zero Or greater than or equal to zero whatever if J Is greater than or equal to zero. I will add to return Whatever character is at I Hope you understand what I'm doing basically If my index is between the start and the end I'm gonna add whatever character it is SI Okay, and the SI is a character. I'm currently looking at This is basically just ensures that whatever you're adding you're not you're removing the left parentheses and the right parentheses Okay, the outer ones All right, let's run this code. Let's see if it works Hopefully if it works ideally it should work because that's what Okay, ooh, okay, something's not right. This is a good thing about programming is that You sometimes some nice things don't work out well. Oh, okay SI J my bad now should work. Let's see No, okay, let me see what what what is the issue that I'm having currently I gotta think Okay, so it's actually as that I and this should j cannot be zero Reason being is that if J were to be zero if J is not zero, right? That if J. Okay, if J is zero then that means that we Are basically adding whatever value it is initially, okay, like the initial whatever we had before This ensures that we're not adding Whatever a value initially that we had before we're not adding the the outer parentheses basically Okay, submit it and it works except it Hope you guys understand the solution rate calm subscribe. I'll check you guys later