 What's up guys, my name is Michael and welcome to my YouTube channel. Today we are going to go over the problem transform the expressions on spage. So basically you are given an algebraic expression of brackets and operand. The operands are plus minus multiplication division and power and you're given brackets, right? And basically we need to transform this expression. I'll show you guys an example. So basically in the first example, a plus b times c. Okay, we want to transform this expression. So this output give us a, b, c, multiply and plus. And basically what this output a, b, c is, they're just taking whatever value before. So like these these letters that was given to us. And they take whichever they print these out first, right, a, b, c, and then they print out the multiplication. So this multiplication operand. And this is, they put this one first because it's between these brackets, right, these parentheses. They put this one first, this multiplication, and then later on they put this one, the addition side afterwards. So that's what this expression gives us. Okay. So now let's look at another one. The reason why they give us this first is because these a, b is first in the parentheses first, right? So we have this parentheses first, parentheses first. So that's why we have they print out a, b first. So that's what this a, b goes here. Then after that they have this plus operation. So that comes in second. Right. And then now we have to evaluate this parentheses, the next parentheses. So this next parentheses is z plus x. And the letters in this parentheses is z is z and x. So we are going to print out z. And we're going to print out x. Then later we're going to print out this operand of plus. So it prints out this plus. And then finally we print out the actual multiplication, because that's the final parentheses and the final one for having both of these. So that's what this does give us. That's what this gives us. It gives a b plus z x plus multiplication. Okay, I hope you guys understand the gist of this. Let's actually go over the final one, I think the final final one, the final test case, which is so what does this give us first. So let's first look at the outer parentheses. So we have an outer parentheses here. And this outer parentheses gives us this multiplication first. But first of all, we have to evaluate left to right parentheses first. So let's look at the left parentheses. So the left parentheses, we have this left parentheses first. So let's actually do this evaluate this first. So in order to do that, we're going to print out the letters. So a t, right. So these letters, a t, and then we're going to print out the operation of this that has so this is going to be a plus. Then later on, what does it do? We have to do evaluate the second operation, which these parentheses, and these parentheses are B, a plus C, right. So then what we're going to do is we're actually going to print out the letters that correspond to it. So first we're going to print out the letters A, B, C, and then we're going to put a plus sign, right, ABC, the plus sign for these operations. And then later on, we're going to have another plus sign because this plus sign is this one. This was the plus sign, right, then we have another plus sign. Because we have two plus signs, right. So this plus sign was originally for this one. This plus signs. And now we're going to add another plus sign, right, because that's two plus signs. So we have B plus plus B, a C plus plus. And then later on, we need to evaluate this parentheses. And this parentheses is CD plus, right, because we have the letters first, we put display the letters and then the operation plus. Then and then we need to have a carrot sign. So we have to put a carrot sign because after evaluating both of these operations. We have this one, this carrot sign. So we have to put that at the end. Okay, so now we have B, a C plus plus CD plus and then carrot sign, because that's the final thing. And then now to top it all off, we need to add the final sign, the final operand, which is this multiplication one right here. So we're going to display that at the end. So in the end, we get a T plus B, a C plus plus CD plus carrot sign multiplication. So that's the end result. And that's what they get here. Right. So I hope you guys understand the gist of this. And then I'm going to explain how to do this problem. So the only thing you really need to know to do this problem is actually the stack data structure. So let's say we're going back to the original. So what I'm going to do is I'm going to create a stack. Basically, what I'm going to do is I'm going to loop through this whole string. So there's a string, right? This is the input string. I'm going to loop through this whole string. Okay, every time I see a operand, I'm going to push it onto the stack. Okay, so every time I see a plus multiplication, minus division, stuff like that, I'm going to push on to the stack. When I see end bracket, right here, I'm going to pop the first part off of the top of the stack, and I'm going to add it to my answer. Okay, so I'm having an answer string that's going to be the answer that I'm going to return. Right? So this answer is going to initially be empty. Otherwise, any other ones, I all I have to do for that is I just have to add it to my answer. So let's actually go just go over a basic test case and how this would work. So we read through this, we first see an open bracket, and then we do nothing, right? We just break. We don't do anything. Nothing happens. Okay, we get to an A, and this is not an operand. It's not any of these operands. So what I'm going to do is I'm going to add it to answer. So answer is going to become, that's the answer here. Answer is going to become a right. Now I'm going to get to the this operand. This is a plus sign. So what am I going to do here? I'm going to add it to my stack. So this to add it to the stack, I'll just add it to here. So I push this on to the stack. Okay. Now I get to this operand. This operand is a parentheses. I'm not going to do anything, right? Because it's a left frenzy. So I'm not doing anything. I just break. All right. Let's go to the next one, the B. This is a letter. So what am I going to do? I'm going to add it to my answer. I'm just added to a pen to the answer. Okay. So my answer is AB. And then I'm done with this. Now we are at this operand. Okay. What's my bad? We're at this operand multiplication. This is an operand, right? This is a multiplication. So what am I going to do? I'm going to add it to my stack. Okay. So my stack is multiplication now. Okay. Okay. Now, now we are at C. C is a letter. So I'm going to add it to my answer. And that would be that it. Now we are at the final operand of parentheses. So at this point, what I'm going to do is I'm going to pop from the stack and add it to my answer. So because if I pop from the stack, remember, every time we add an operand, we push it onto the stack. When we reach a closing parentheses, that means that we need to get whatever the closest operand that was just added before the parentheses, right? So that's why we use a stack because that the stack gets us the first closest, the last operand that was just added before the print. So now when we have a parentheses, we just pop from the stack. So now the stack used to have a multiplication. Now it's gone. So now it's popped and we're going to add it to our answer. So that's this. Okay. Okay. And now we have the final one, which is that final parentheses. Now, because we have fine parentheses, we're going to pop from the stack. And this is stacks, not empty. So it's plus we pop it. And we add it to our answer. So our end answer is going to be ABC multiple multiplication plus. Okay. So that's basically the gist of how to do this problem. I'm going to explain the code now to you guys. I'm sure how that works. So in this code, it's basically the exact same thing of how I explained it. We first read in the test case of T. And that's just the number of test cases that we're reading. Then we read in our string, str and that's just the string, we have an string answer that keeps track of the with the current answer that we're adding, right? Then we I have a stack. I call it f, but this is just f was just the quickest letter names, you know, to write, you call it s or whatever. Okay, now we're going to read it through all of our read through all the letters in our string. So that's what this index does is looping through the string. Okay, now I have a switch statement and the switch statement is just go through every single case by case of the letter. Okay, so if it is a left parentheses, I just break because that doesn't matter, right? The left parentheses doesn't matter. Okay, so now these cases plus minus division multiplication, exponential, right? All these cases all have the same result. And in these cases are all the operand for these you just push it onto the stack, like I showed you guys, right? For every any type of operand for these, we just push it on the stack. And I break because of first switch statements. If you don't break it just goes on to the next case. Right. And that's not a good idea. Okay, now at the closing parentheses, we get the top of the stack, right? And we add it to our answer. So we remove the top of the value of our stack and we add it to our answer. And then C++ popping doesn't actually return the value. So yeah, I had to call top and then add it and then pop it. I don't know. That's just that's just how C++ works. But anyway, basically, we just removed from the top value topmost value of the operand that was just added before the parentheses. And we added to our answer. Okay, after that, I break because if you don't use a break statement in C++, they would just go on to the next case, regardless. So that's not a good idea. All right, default is when you have any letters that are possible. For those you just add the add your answer to your string, and then you break. Okay. And then after that, I just print out my answer string, see out answer string, and that's pretty much the whole gist of the code. Hope you guys enjoy this video rate com subscribe. I'll check you guys later.