 What's up guys my is Michael and welcome to my youtube channel today We're gonna do another lead code challenge called remove all adjacent duplicates in the string basically you're given a string And you got to remove every duplicate Every duplicate character in the string every adjacent duplicate actually so basically if there is a repeating a bb a ca BB is repeated remove that once you remove that you see a and a is repeated But you got to remove that and that ends with ca so What you have to do is is that You need to know how to use a stack. That's how you how that's how you would do this question I'm gonna assume you guys already did this so I'll explain how I did it first. You got to create a stack called a Stack whatever stack. Let's call it data. We're gonna go through every value in the string every character in the string Yeah, I'll use a regular It's actually better if we don't use this way anymore Because in the new CFC plus plus version section better if you go through by character using a for each loop But what is to do it for now? Okay? First we're gonna do is we're gonna check if the stack is empty if the stack is empty We are going to push the current Character onto the stack so we're gonna do your stack push data that push s at I So that's what I'm gonna do It's actually we want to remove every duplicate right so first We create a stack and if it's empty, we're gonna push the current value onto the stack otherwise We need to check We have to check if the top part the The current value on the top of the stack Which is the top part on the top of the stack Which is the latest value on this top the top of the stack, right? If it's equal to our value Then we got to remove it we're gonna remove it. Okay, we're gonna remove the value from our stack The reason why is because this is going to prevent duplicates Essentially is if we keep track of every current value and then from our stack If it's equal to the current value from our string and we remove the one on the stack in the end There will be no duplicates in our stack. That's really how it works Yeah, we're gonna pop it If it's not equal to We're just added. We'll just push back push the value at it Okay, so if the top part of our stack the latest Value on our stack is not equal to the Character we're currently at then we'll just add the new a new character. Okay, so that's how you do this Yeah, so after this is done now essentially is after this whole For loop is finished our stack is gonna have no duplicates in it now We have to go through our whole stack and We need to add it to our a new string essentially So I'll do string to return Because our stack has No duplicates of characters now we have to add add each value to the To our new string that we're turning because this this method requires to return string So return to return In here, I'm going to get the first part of our the first part of our the top of our stack. So top I'm gonna get the current value on our stack the top of our stack and I'm going to add it to to return We're gonna add it to the front of to return actually the reason why we're adding to the front is because Because because of stacks how you keep adding the current value onto the top of the stack, right? if you want to If you keep removing the value from the top of the stack the top of the stack is going to be the end value of our string So we have to add it to the beginning Okay, because the top of the stack is basically in reverse order because we're adding to the top of the stack every time So essentially, this is a new reverse order. So that's why we're what we're gonna do is we are going to Go through our whole stack and basically add it to the front of our string that we're turning and that will basically Reverse the order back to its original After that, I'm gonna do data that pop. So I remove the top value and this should work On the code. We got CA CA submit and it got accepted. Uh, this is not as fast as some of 15% of the C++ Submissions, I can't even see the solution, but essentially this is a way to do it another way you could do it is actually to Let me see You could reverse the string so you could Could add it to the back and then reverse the string I Could try that. Let's see instead of creating and adding it to the front. So You could do this to return plus equal Okay, and then another thing we could reverse to return begin It's basically the same thing But now we're not adding to the front Let's see if this is faster or not. I think it would be slower. Honestly. Oh got faster. Wow. Okay So yeah, another thing you could do that you go reverse you could add it to the back of the new string and then reverse it Let's see other people solutions Beats 100% Okay, so they basically did exactly what we did They use pushback. Oh, okay, so they push back The top then they popped then they reverse. Okay, so another thing we could do that that might be better Pushback might be actually better than just adding plus equals We could try that Well, it's okay. If it's not empty and I as equal to the top then pop Yes, that basically what we did. Oh, yeah, you could do you could use this solution also Yeah, so that's basically how you do this question Use a stack keep track of the current value and then if it's equal to the current value you pop it and I'll basically get rid of duplicates Ray com subscribe. I'll check you guys later. Peace