• 2 weeks ago
Transcript
00:00Next question, which we will talk about is valid parenthesis. What is the name of the parenthesis? We have called
00:04the parenthesis in every way.
00:07Those characters that are used in brackets, we have called parenthesis in this question, we have called parenthesis and these
00:15square brackets.
00:17So, we are talking about these 6 particular characters whenever I use parenthesis word.
00:22So, I have to check that if I have a string. What is the string? Some kind of string.
00:28So, the set of characters in the string, if they are just my parenthesis characters, i.e. brackets,
00:36do they form a valid parenthesis or not? What is a valid parenthesis?
00:42Valid parenthesis or input string is the one that, after opening brackets, we always have the same type of closing
00:48brackets.
00:48This is a valid one. healthy type,eeee. If i have opened open one, then we should close this from
00:53this.
00:53If i have opened this and closed this from this, then i opened the steels of it to this.
00:58Then i opened from curly braces and closed this from the parenthesis from this Yarns.
01:03So it will be wrong, i am wrong. Yes this is only right, but i mean only in this way.
01:07or this way opening closing or this way opening closing will be right.
01:12What should the second condition be?
01:14The second condition is that my opening bracket should also be in the correct order.
01:22If I have added another open bracket here, then I have added a square bracket
01:28then I can't do that.
01:30I will add parenthesis in the closing bracket, then I will add another parenthesis
01:34and then I will add this.
01:36This will be a wrong format.
01:39It will be a right way that when I have added opening,
01:42the last opening, the closing bracket will also be after that.
01:47Then it should be closing.
01:50Then it should be closing.
01:52Or if I have made a structure in which I have added normal parenthesis,
01:58then curly brace, then square bracket.
01:59So, at closing time, first of all, I have added a square bracket so that it will be a pair.
02:04Then I have added a curly brace so that it will be a pair.
02:08Then I have added parenthesis.
02:10If I have written this structure,
02:12I have added a bracket before but then I have added parenthesis.
02:15Then I have added this.
02:16Now we can say that it will be a match.
02:19But the match didn't happen.
02:20How did it happen?
02:21Because this is closed.
02:23But this is not closed.
02:24Because the curly brace can be closed.
02:27So, this is correct.
02:29This is correct.
02:30This is correct.
02:30And this is correct.
02:32So, I have to satisfy the second condition.
02:34The third condition is that every closing bracket has a corresponding opening bracket of the same type.
02:39So, if the closing bracket is this, then opening should be this.
02:44If the closing is this, then opening should be this.
02:47If the closing is this, then opening should be this.
02:47If the closing is square bracket, then opening should be this.
02:50The three conditions will satisfy me.
02:53That will be a valid string of my parenthesis.
02:56Now, I have to take some examples of this.
02:58When I talk about this string,
03:00In this one, this one is closed.
03:03This one is closed.
03:04This one is closed.
03:05This one is closed.
03:06So, this is a correct statement.
03:08This is my valid string.
03:10I will return true.
03:11Next example.
03:12In the beginning, I have my opening parenthesis.
03:14Last one is opening parenthesis.
03:16So, this is also a valid string.
03:18I will return true.
03:20Let me see this example.
03:21When I start, this is opening parenthesis.
03:24But I close my square bracket.
03:26This is wrong.
03:27So, I will return false.
03:30I will return false.
03:30Another example.
03:31In the beginning, opening and closing are the same parenthesis.
03:36But there is a problem.
03:37Closing is first and opening is after.
03:41This is also wrong.
03:42Opening always comes before.
03:44Closing always comes after.
03:46Then a valid parenthesis is formed.
03:48So, this is also a wrong string.
03:50Now, the question of valid parenthesis is solved.
03:54In fact, this is a normal iteration.
03:56We can solve it with a normal loop.
03:57We can solve it with a recursion.
03:59We can solve it with a recursion.
04:00There are many ways.
04:01Here, because we are reading a stack chapter.
04:03I will solve it with a stack.
04:05And the way we solve it will be an optimized way.
04:08We will solve it in linear time complexity.
04:12First, let's discuss this approach.
04:14In this approach, I have to imagine that whatever I am going to test for the string.
04:18Let's assume that my string is something like this.
04:20First, opening parenthesis.
04:22Then, I have a curly brace.
04:23Then, closing bracket.
04:25Then, closing bracket.
04:25Then, this.
04:26After that, it can also be like this.
04:28Then, another parenthesis.
04:29Then, this.
04:30This also has a right string.
04:32Because it is also properly closed before it.
04:34After that, it is closing.
04:37So, I have to return true to this.
04:39I will analyze my process for the string.
04:42I will use this question.
04:45I will use a stack.
04:48So, I will create a stack.
04:50What am I going to do with this?
04:52I will imagine this string in a burger form.
04:56I can say that I can imagine this string.
05:00I will go to this string.
05:02Then, I will go to this string.
05:03Then, I will go to this string.
05:04Then, I will go to my closing.
05:07Then, an opening.
05:08Then, the string will go to the contracting.
05:10Then, a string will go to the Snail.
05:11As I have a case with an opening bracket,
05:15Which one ing buyers of those brackets,
05:17then, I will push within the stack.
05:19Otherwise, this string has an open bracket.
05:21I would push into the stack.
05:23Otherwise, I have to dagara정头ρ girdle.
05:25then I am pushing in the stack.
05:26This is my opening bracket.
05:27I pushed into the stack.
05:28I released that my stack.
05:29If , this will be been stored into the characters in the stack.
05:31I pushed into a stack of openings in the stack.
05:35Then, once again, sais
05:36it has an opening little.
05:37What will I do again? I will push it into the stack
05:40Then there is another opening
05:42In fact, I will talk about my normal string
05:45Then I will push it into the stack
05:50Then I will push it into the stack
05:51Then I have a square bracket
05:53Now I have a closing
05:55As I have a closing
05:57I have to perform another step for closing
06:00I will check for closing brackets
06:02I will check the stack at the top
06:07It is making a pair
06:09If it is making a pair
06:11Then things are fine
06:12If it is not making a pair
06:14In this case, I will have a false condition
06:17I mean, the whole string of my characters
06:21Or my parentheses
06:22Is wrong
06:23How can I understand this?
06:26Let's assume that I have a string of characters
06:27Where my closing brackets
06:29If it is making a pair
06:31It will also be the first character
06:34I mean, the first opening
06:36It can be a pair
06:38I will explain it with a small string
06:41Let's assume that I have something like this
06:45If the first closing is coming
06:48I mean, the pair is the opening
06:51It means that it will be the first one
06:53If it is making a pair
06:54With the second number
06:55If it is making a pair
06:55Then the pair
06:58With the first opening
07:00Which will become the first one
07:02The pair will become the last one
07:05With the last one
07:07Opening
07:08The pair will become the last one
07:10Opening
07:10So this way
07:12The pairs are made
07:12So what can I say
07:14If I have a stack
07:15In the stack
07:16I have opened this opening
07:18Then I have opened this opening
07:20So for this closing, the opening pair will exist at the top of the stack on the top of the
07:26stack.
07:27And I will analyze the other string. I have taken this, then I have taken curly braces, then I have
07:32closed this.
07:34If I take the stack, first of all, the opening of the stack will come.
07:38Then the opening of the stack will come.
07:41Now when I talk about closing, the closing matching pair will be at the top of the stack.
07:47Because in the last, it will be analyzed.
07:51In the last, it will be analyzed. In the last, it will be run operation.
07:54And in the last, the curly braces will be pushed at the top of the stack.
07:58So if I have to find the pair, what will I do?
08:02I will check the top of the stack.
08:04If the pair exists at the top of the stack, if the opening pair exists at the top of the
08:11stack,
08:11then my valid parenthesis is running.
08:15But if we consider here, there is an invalid condition.
08:19This is mine.
08:20Here I have the opening pair.
08:23So I push the square here.
08:25Now when I compare it with the square, then what happens?
08:28This is not a pair.
08:30Meaning, for closing braces, the pair should always be the same pair.
08:34But here there is a square bracket.
08:36So the square bracket is coming here.
08:38I know that my parenthesis is not valid.
08:40So in that case, I will return it with false.
08:43But yes, if the closing is coming here and the top of the stack, then it is valid pair.
08:48If the closing is coming here and the top of the stack is also valid pair.
08:53If the closing is coming here and the top of the stack is also valid pair.
08:57If I have the golden pair in three conditions, it will come here and the top of the stack is
09:03already coming here.
09:05And then when I have the square brace in closing and the top of the stack is already coming here.
09:11Or if I have the other condition, I can close with the parenthesis of closing and the top of the
09:15stack is also coming here.
09:17So, in these three conditions, my stack or my string is valid parenthesis.
09:23The other conditions have never been broken. So, my string will not be valid parenthesis.
09:28So, when closing comes, I will check the closing bracket for the top of the stack.
09:34If it is the right pair, then it is okay. Then I will analyze it for the next one.
09:39And if it is not, then I will return false.
09:41So, first of all, I will analyze this closing bracket for the square bracket.
09:46For this, I will check the top of the stack.
09:49So, here is the opening bracket here.
09:51So, in this case, the condition is true.
09:54As this condition is true, what can I do?
09:56My top, I can pop it.
10:00Pop it means that I will remove it from the top of the stack.
10:04I will remove it from the top.
10:05I will remove it from the top.
10:06Because the next one will come.
10:08Closing parenthesis.
10:09You will also find out the pair.
10:11And it can only check the top.
10:13So, for the square bracket, the pair was successfully obtained.
10:16So, I can remove it from the pair.
10:18Now, I will increase this bracket.
10:21For this bracket, I will check that the pair exists on top.
10:26So, the opening bracket exists.
10:27This pair exists.
10:29So, I will remove it from my stack.
10:30And then I will check this one.
10:32What are we?
10:33Opening bracket.
10:34We can't check the opening bracket.
10:36We simply push.
10:37I pushed this opening bracket at the top.
10:40Then I will come to the next element.
10:42The next element is the closing bracket.
10:45Now, you will check the closing bracket.
10:45The closing bracket will check that it exists on top of the stack.
10:48So, it exists on top of the stack.
10:52So, this pair exists on top of the stack.
10:54So, I will remove the pair here.
10:55I will do the pop.
10:56Then what will we do?
10:57We will go to the next element.
11:01This will also check if the pair exists. If it exists, let's pop it.
11:06What did I do in the last? I did the pair.
11:09Now let's assume that this is the string.
11:13In the beginning, there is another parenthesis.
11:18In the last case, I would normally pop it.
11:23But in the last case, my single single starts now.
11:28When all my steps are completed,
11:31when I reverse the string,
11:34I have to check what my stack is empty.
11:38Because if the stack is empty,
11:40it means that in the beginning,
11:42there were some extra opening parenthesis,
11:44opening braces,
11:45opening brackets.
11:47There were openings that were never closed.
11:50Like this blue one,
11:51there was no closing pair of it.
11:53It was already in the stack.
11:54So if the stack is empty,
11:56then the string will become invalid.
12:00It means that there is something incomplete.
12:03But if the stack is empty,
12:05then I can return it to the last case.
12:07So how do I write code?
12:09In the code,
12:09something like that,
12:12I will go to the first case,
12:13I is equal to zero,
12:14I less than string length.
12:18And every time,
12:19I will do I,
12:21plus, plus.
12:22I have a character in the ith position.
12:27I will remove it.
12:28If the character is an opening character,
12:32then I will put it in the stack.
12:34And if it is a closing character,
12:36then I will find two conditions.
12:39First of all,
12:40the valid pair is being made with my stack
12:42or with the peak.
12:45If it is being made,
12:46then we will remove the opening pair from the stack.
12:50If it is not,
12:51then I will return false.
12:54False return means that
12:55here I have an invalid condition.
12:59In some way,
13:00there will be a loop
13:00which will go to each other characters.
13:03If it is opening,
13:04then it will push.
13:05If it is closing,
13:06then it will find the pair.
13:07The pair will not get the pair.
13:08So it will return.
13:09False pair will get the pair,
13:10then it will pop inside my stack.
13:12And in the last,
13:13I will check another extra condition.
13:15If my stack is empty,
13:19then I will return true.
13:22Yes, the string was valid.
13:23But if the stack is empty,
13:24But if the stack is empty,
13:25then I will return false.
13:27I will return false.
13:27There was some extra opening parentheses
13:29or in the beginning
13:30or somewhere in the middle
13:32or somewhere in the middle
13:32that exist in my stack.
13:35So in this case,
13:35the stack is invalid.
13:37My string will also be invalid.
13:39So in that case,
13:40I will return false.
13:41So now we will write code.
13:43In fact,
13:44before I will advise you
13:45that I will take another string
13:47I will take a string.
13:48You have to take a string.
13:49You have to take a string.
13:52You have to take a string.
13:53One time,
13:53I will run dry and run.
13:54It will be the same.
13:55Using the code.
13:56Or using the three steps.
13:58And the other one,
13:59we have to take a string.
14:00This should be returned true.
14:03And for this string,
14:05I have to open it.
14:10I have to return false.
14:10For this string,
14:11I have to return false.
14:13For these two,
14:14I will try to write code
14:14before I will try to write code.
14:17And after I will try to try to write code.
14:18If we try to write logic
14:18it will become easier.
14:21Then I will try to write code.
14:22And then I will try to write code.
14:24Good night.
Comments