1
00:00:01,710 --> 00:00:02,400
Hi, everyone.

2
00:00:02,430 --> 00:00:06,310
So today we are going to solve this problem, valid parentheses.

3
00:00:06,840 --> 00:00:09,180
OK, so the problem statement is very simple.

4
00:00:09,210 --> 00:00:11,440
So we will be provided with a string.

5
00:00:11,880 --> 00:00:17,640
OK, so the input will be a string and the string can contain only six type of characters.

6
00:00:18,480 --> 00:00:22,590
OK, so these are the six type of characters that the string will be made up of.

7
00:00:22,620 --> 00:00:23,040
OK.

8
00:00:28,040 --> 00:00:33,920
So these are six characters and the string will contain only these six characters.

9
00:00:34,070 --> 00:00:38,060
OK, so we have to tell whether the string is a valid string or not.

10
00:00:38,120 --> 00:00:44,060
OK, so for example, if they input this, so this is a valid string, so our output should be true.

11
00:00:44,240 --> 00:00:48,230
OK, so we have to redone that type of the function will be OK.

12
00:00:48,240 --> 00:00:49,100
So we have to return.

13
00:00:49,250 --> 00:00:55,820
True or false, whether the parentheses is valid, whether balances are balanced or not balanced.

14
00:00:56,030 --> 00:00:57,890
So in this case, our output will be true.

15
00:00:58,430 --> 00:01:01,400
So in this case, so these two are merging again.

16
00:01:01,400 --> 00:01:02,360
These two are matching.

17
00:01:02,360 --> 00:01:03,470
These two are matching.

18
00:01:03,770 --> 00:01:04,970
So this is string.

19
00:01:05,000 --> 00:01:06,740
These parentheses are balanced.

20
00:01:06,740 --> 00:01:08,040
So our output will be true.

21
00:01:08,810 --> 00:01:13,250
Now, in this case, our opening record is different and our closing record is different.

22
00:01:13,550 --> 00:01:19,790
OK, so our output will be false here since the opening and closing record do not match.

23
00:01:19,820 --> 00:01:23,950
OK, opening and closing records are different now in this case.

24
00:01:24,350 --> 00:01:26,420
So this is my opening record.

25
00:01:26,540 --> 00:01:30,110
Again, we have an opening record and then we have a closing bracket.

26
00:01:30,120 --> 00:01:30,900
So this is wrong.

27
00:01:31,550 --> 00:01:33,900
So this should be like this only.

28
00:01:33,920 --> 00:01:38,010
OK, so that it matches the opening bracket so the output will be false.

29
00:01:39,860 --> 00:01:44,370
Now, in this case, opening bracket again, opening bracket, now the closing bell.

30
00:01:44,630 --> 00:01:47,120
So the closing bracket and opening bracket.

31
00:01:47,120 --> 00:01:48,140
So they are matching.

32
00:01:48,410 --> 00:01:49,520
So we will move ahead.

33
00:01:49,790 --> 00:01:51,710
So now this is closing brackets.

34
00:01:51,710 --> 00:01:54,320
So closing bracket is matching with the opening bracket.

35
00:01:54,590 --> 00:01:56,170
So our output will be true.

36
00:01:56,510 --> 00:02:02,180
OK, so that is the meaning of balanced parenthesis, balanced or you can say valid parenthesis.

37
00:02:02,510 --> 00:02:04,250
OK, balanced or valid.

38
00:02:04,430 --> 00:02:09,860
So our output will be a string and the return type will be a boolean value, true or false, whether

39
00:02:09,860 --> 00:02:11,330
string is balanced or not.

40
00:02:11,630 --> 00:02:16,820
OK, so how are we going to solve this problem so we can solve this problem with the help of Steg.

41
00:02:18,380 --> 00:02:21,190
OK, so how stacked will help us to solve this problem?

42
00:02:21,410 --> 00:02:22,830
So let's take an example.

43
00:02:23,180 --> 00:02:25,460
So let's take this example.

44
00:02:26,640 --> 00:02:31,440
OK, so let's take a bigger example to help us understand how the will work.

45
00:02:31,980 --> 00:02:35,100
OK, so let's take the example of a valid string.

46
00:02:35,100 --> 00:02:43,500
So we have opening record again, a different opening record, then a closing record, let's say, again,

47
00:02:43,500 --> 00:02:45,920
opening Beckert, closing record.

48
00:02:46,530 --> 00:02:48,100
So again, opening.

49
00:02:48,420 --> 00:02:50,900
So this is closing and this is closing.

50
00:02:50,980 --> 00:02:54,230
OK, so this is an example of a valid string.

51
00:02:54,240 --> 00:02:55,320
So this is valid.

52
00:02:56,290 --> 00:03:02,050
How this is valued, you can see opening and closing, they are merging again, opening and closing,

53
00:03:02,050 --> 00:03:06,130
they are matching, opening and closing, they are matching and again, opening and closing, they are

54
00:03:06,130 --> 00:03:06,570
matching.

55
00:03:06,790 --> 00:03:10,270
OK, so basically our output should be true in this case.

56
00:03:11,400 --> 00:03:17,810
Now, how we can use the help of Streck, so initially what we will do, so I will create a strike.

57
00:03:18,000 --> 00:03:19,950
So initially our strike will be empty.

58
00:03:21,030 --> 00:03:24,300
OK, so this is my steg initially, the deck is empty.

59
00:03:24,510 --> 00:03:26,760
Now what we will do, we will iterate over the string.

60
00:03:26,790 --> 00:03:29,830
OK, so I am standing here, OK?

61
00:03:29,850 --> 00:03:33,870
So if it is an opening record, then I will push it inside the stick.

62
00:03:34,350 --> 00:03:36,150
OK, so what are the rules.

63
00:03:36,750 --> 00:03:38,370
So rules is if.

64
00:03:39,300 --> 00:03:40,260
Opening record.

65
00:03:42,220 --> 00:03:45,850
OK, so if you have an opening parenthesis, opening bracket, then push.

66
00:03:47,370 --> 00:03:48,690
So this is very simple.

67
00:03:48,870 --> 00:03:53,810
OK, so rule number one, if the beckert is opening, then they actually push inside the stack.

68
00:03:54,390 --> 00:03:55,410
Now we will come here.

69
00:03:56,680 --> 00:04:00,460
So, again, this is an opening record, then directly push inside the deck.

70
00:04:01,640 --> 00:04:05,640
Now see, so now we have a closing bracket, so rule number two.

71
00:04:05,900 --> 00:04:09,990
So if the bracket is a closing bracket, then what will do?

72
00:04:10,010 --> 00:04:15,140
So if the bracket is disclosing bracket, then at the top of the stack.

73
00:04:17,620 --> 00:04:19,089
The record should be this one.

74
00:04:19,149 --> 00:04:20,320
OK, semillon.

75
00:04:21,970 --> 00:04:28,930
If the closing bracket is this one, then at the top of the stack, our opening bracket should be this

76
00:04:28,930 --> 00:04:29,160
one.

77
00:04:29,770 --> 00:04:35,920
If the closing bracket is this one, then at the top of the stack, our opening records should be this

78
00:04:35,920 --> 00:04:36,160
one.

79
00:04:36,580 --> 00:04:42,730
OK, now, if the opening if the top of the stack is not this one, then I will simply return false.

80
00:04:43,660 --> 00:04:47,430
Similarly, if the opening bracket is not this, then I will return false.

81
00:04:48,280 --> 00:04:55,150
If the closing bell is this and opening in the top top of the stack is not this character, then I will

82
00:04:55,150 --> 00:04:55,810
return false.

83
00:04:56,650 --> 00:05:03,070
OK, so in this case we have this closing bracket now at the top of the stack.

84
00:05:03,070 --> 00:05:05,440
We have its corresponding opening bracket.

85
00:05:05,590 --> 00:05:06,580
Then what we will do.

86
00:05:07,120 --> 00:05:08,620
So this is the case.

87
00:05:09,250 --> 00:05:11,890
We have this closing bracket and the top of the stack.

88
00:05:11,890 --> 00:05:13,390
We have this opening bracket.

89
00:05:13,540 --> 00:05:16,360
So if this is the condition that we will do so.

90
00:05:16,360 --> 00:05:19,720
I am writing here, if this is the condition, then simply pop.

91
00:05:20,300 --> 00:05:22,870
OK, so we will remove this part.

92
00:05:24,090 --> 00:05:30,580
And now let us move ahead, similarly here we will pop and similarly here we will pop.

93
00:05:30,600 --> 00:05:34,620
And in the last part we will return for its OK and that's we will return fault.

94
00:05:35,650 --> 00:05:42,070
Now, let us remove this, OK, so move ahead, we have opening bracket since we have an opening bracket,

95
00:05:42,070 --> 00:05:43,270
likely push inside the.

96
00:05:44,050 --> 00:05:45,760
Now we have a closing bracket.

97
00:05:46,000 --> 00:05:47,530
So we have this closing.

98
00:05:47,530 --> 00:05:49,510
Very good logic, the top of the stack.

99
00:05:49,750 --> 00:05:55,480
So top of the stack is we have a similar corresponding its corresponding opening record.

100
00:05:55,750 --> 00:05:56,860
So this is the case.

101
00:05:57,550 --> 00:05:58,930
This is my closing bracket.

102
00:05:58,930 --> 00:06:03,700
And the top of the stack I have, this is its corresponding opening bracket.

103
00:06:03,700 --> 00:06:05,080
Then what we have to do, Bob.

104
00:06:05,380 --> 00:06:07,000
OK, so we will pop.

105
00:06:08,530 --> 00:06:15,760
Now, let us move ahead so I have this opening record now at the top of the stack, I have its corresponding

106
00:06:16,150 --> 00:06:17,000
opening record.

107
00:06:17,020 --> 00:06:20,450
OK, so OK, so sorry, we have opening backward.

108
00:06:20,470 --> 00:06:23,110
So if we have opening record, we will simply push.

109
00:06:23,710 --> 00:06:24,670
So this is.

110
00:06:26,060 --> 00:06:33,290
Might this opening record no, I have a closing record, OK, so I have this closing bell now at the

111
00:06:33,290 --> 00:06:38,040
top of the stack check whether it's got a smartening opening record, it is not as present or not.

112
00:06:38,300 --> 00:06:40,520
So it's corresponding opening back of this present.

113
00:06:40,520 --> 00:06:42,680
Then what we have to do, we have to pop.

114
00:06:42,920 --> 00:06:46,190
OK, so I will pop this part now.

115
00:06:46,190 --> 00:06:46,760
Move ahead.

116
00:06:47,510 --> 00:06:49,340
So I have this closing record.

117
00:06:49,730 --> 00:06:50,780
So this is the case.

118
00:06:50,780 --> 00:06:56,090
I have this closing record now at the top of the stack, whether check whether it's corresponding opening

119
00:06:56,090 --> 00:06:58,190
because there are not OK.

120
00:06:58,430 --> 00:07:02,000
So it's corresponding opening bracket is there at the top of the stack.

121
00:07:02,210 --> 00:07:04,710
If it is there, then we have to do we have to pop.

122
00:07:05,510 --> 00:07:11,460
So I will pop and then we will reach the end of the string and that will strike is empty.

123
00:07:11,480 --> 00:07:18,860
OK, so if our stack becomes empty after traversing the whole string, if our strike is empty, then

124
00:07:19,110 --> 00:07:20,270
we will return through.

125
00:07:20,660 --> 00:07:23,270
OK then only we have balanced parenthesis.

126
00:07:24,510 --> 00:07:28,750
OK, if the state was not empty, so in the early part, we will return false.

127
00:07:29,400 --> 00:07:33,180
So when the state will not be empty, for example, it is considered a situation.

128
00:07:33,450 --> 00:07:38,640
So let's say our string is basically, let's say only this much.

129
00:07:39,160 --> 00:07:41,510
OK, so we have only a single correct string.

130
00:07:41,700 --> 00:07:42,990
So in this case, what will happen?

131
00:07:42,990 --> 00:07:45,400
I will reverse the string fence.

132
00:07:45,430 --> 00:07:46,560
This is an opening bracket.

133
00:07:46,560 --> 00:07:49,130
So initially my map, initially my state will be empty.

134
00:07:49,410 --> 00:07:50,760
So it's an opening bracket.

135
00:07:50,760 --> 00:07:53,400
Then I will push, then I will move ahead.

136
00:07:53,400 --> 00:07:55,370
So I westing will finish.

137
00:07:55,740 --> 00:08:00,000
Now we will check to since the state is not empty, I will return false.

138
00:08:01,020 --> 00:08:04,680
So in this case, I will return false because the stack is not empty.

139
00:08:06,520 --> 00:08:09,010
OK, so these are very simple rules.

140
00:08:09,550 --> 00:08:13,630
OK, so first rule is if you have opening record of any kind.

141
00:08:14,800 --> 00:08:21,730
Then you can likely push if you have opening record of any kind, so opening record can be of obviously

142
00:08:21,730 --> 00:08:22,370
three kinds.

143
00:08:22,690 --> 00:08:25,540
So this one, this one and this one.

144
00:08:25,820 --> 00:08:31,300
So if you have any of these three characters, then you will directly push inside the stick.

145
00:08:31,960 --> 00:08:36,100
Now, if you have closing record, so closing record can be of three types.

146
00:08:36,460 --> 00:08:38,200
OK, so closing bell.

147
00:08:38,200 --> 00:08:39,100
Good can with this one.

148
00:08:39,490 --> 00:08:42,669
Closing bell, good can with this one and closing record can with this one.

149
00:08:43,650 --> 00:08:50,970
If the closing record is this fun, then at the top of the stack, so at the top of the stack, you

150
00:08:50,970 --> 00:08:53,520
should have its corresponding opening record.

151
00:08:54,030 --> 00:08:59,220
So if you have its corresponding opening record, then pop from the stack.

152
00:09:00,590 --> 00:09:03,440
Otherwise, you can directly return false.

153
00:09:05,260 --> 00:09:09,570
Are there ways you can like it unfolds and similarly with these characters?

154
00:09:09,630 --> 00:09:14,910
OK, now let us see the example of Foyt, so let us take Invalid's String.

155
00:09:15,240 --> 00:09:24,300
So let's say our string is basically so this is opening record and this is Lizzi closing record.

156
00:09:24,450 --> 00:09:25,970
Now let us out of the string.

157
00:09:26,190 --> 00:09:27,280
So at this point.

158
00:09:27,780 --> 00:09:29,180
So this is an opening record.

159
00:09:29,190 --> 00:09:31,880
What we will do, we will push inside this deck.

160
00:09:32,190 --> 00:09:34,110
So I will push the opening record.

161
00:09:34,290 --> 00:09:35,040
Now, come here.

162
00:09:35,850 --> 00:09:40,750
So this is a closing record and closing record is of this one kind.

163
00:09:40,800 --> 00:09:46,080
OK, so if this is the closing record, then at the top of the stack, I should have this one opening

164
00:09:46,080 --> 00:09:46,490
record.

165
00:09:46,770 --> 00:09:48,120
Now check the opening record.

166
00:09:48,360 --> 00:09:51,000
So opening record at the top of the stack is different.

167
00:09:51,210 --> 00:09:53,790
Since it is different, I will likely return for it.

168
00:09:53,970 --> 00:09:57,630
So I will return files, which is the correct output.

169
00:09:58,670 --> 00:10:03,820
OK, so what do we the time and the space complexity, so time, complexity is very simple.

170
00:10:03,830 --> 00:10:09,860
We are just iterating over the string of time complexities when and the space complexity we are creating.

171
00:10:10,540 --> 00:10:12,440
So the space complexity will be on.

172
00:10:12,640 --> 00:10:15,000
OK, now let's write the code.

173
00:10:15,410 --> 00:10:18,650
So basically this problem is present only at code.

174
00:10:18,680 --> 00:10:23,060
OK, then the problem is valid parentheses and these are some examples.

175
00:10:24,130 --> 00:10:30,990
OK, so now let us write the code so what we have to do, so first we have to create a stack.

176
00:10:32,280 --> 00:10:38,870
Now, the tape will store characters, OK, stack monster characters and let's say the name of the stackers,

177
00:10:40,470 --> 00:10:46,660
so let's change the name of the string to be a and then able to strike us as OK.

178
00:10:46,680 --> 00:10:50,430
So what we have to do, we have to simply iterate over the string.

179
00:10:50,890 --> 00:10:53,580
OK, so let's say straight or the string.

180
00:10:55,030 --> 00:10:57,280
We will pick each character, so.

181
00:10:58,680 --> 00:11:00,300
I am outrating over Distin.

182
00:11:01,860 --> 00:11:07,120
Now, our first rule, so if I have opening bracket, then we can actually push.

183
00:11:07,480 --> 00:11:08,670
OK, so.

184
00:11:10,680 --> 00:11:14,400
If we have opening markets opening, Beckett can be of three kinds.

185
00:11:16,030 --> 00:11:22,240
So or a call, the second type of opening record.

186
00:11:23,340 --> 00:11:26,190
And similarly, the third type of opening record.

187
00:11:27,820 --> 00:11:33,250
So if you have any of these three opening records, then what we will do, we will push inside this

188
00:11:33,250 --> 00:11:35,920
track, we will push this character inside the stack.

189
00:11:36,490 --> 00:11:38,260
So as push off a.

190
00:11:40,200 --> 00:11:45,600
OK, now in the L'ESPRIT, now we have closing records.

191
00:11:45,980 --> 00:11:48,420
OK, so if we have closing records.

192
00:11:49,780 --> 00:11:54,560
What we have to do, so first of all, let us check.

193
00:11:54,580 --> 00:11:57,910
So if the stack is empty, then we can simply return false.

194
00:11:58,210 --> 00:12:00,820
OK, so if the deck is empty.

195
00:12:03,060 --> 00:12:06,210
What you can do, we can simply return false.

196
00:12:06,810 --> 00:12:09,090
OK, so this is important to right here.

197
00:12:09,090 --> 00:12:14,390
I will explain it later, why it is important to write this line, OK, that you will get runtime error.

198
00:12:15,060 --> 00:12:17,580
OK, so important otherwise.

199
00:12:21,180 --> 00:12:21,930
Reintegrated.

200
00:12:22,880 --> 00:12:25,470
OK, I will explain it later after writing the complete code.

201
00:12:25,590 --> 00:12:28,100
OK, now what you have to do.

202
00:12:28,380 --> 00:12:31,240
So we have closing the code at this point.

203
00:12:31,810 --> 00:12:32,940
Now let's check.

204
00:12:34,170 --> 00:12:34,830
So if.

205
00:12:35,780 --> 00:12:45,440
Now, let us write elusive, so the elusive part or two, all right, so if HFA is now, we have two

206
00:12:45,440 --> 00:12:46,640
types of closing record.

207
00:12:47,000 --> 00:12:52,300
So I have this kind of closing record, then what I have to do, I have to check the top of the stack.

208
00:12:53,030 --> 00:12:53,780
So if.

209
00:12:55,020 --> 00:12:55,870
Startup.

210
00:12:58,470 --> 00:13:01,320
A consequence, its corresponding opening record.

211
00:13:01,350 --> 00:13:03,870
OK, so its corresponding opening record is this one.

212
00:13:04,980 --> 00:13:09,330
Now, if this is the case, what I have to do, I can I will simply pop.

213
00:13:11,110 --> 00:13:16,300
I can simply park while I can simply pop and in the part I can return false.

214
00:13:18,610 --> 00:13:20,380
OK, so we we will do.

215
00:13:21,660 --> 00:13:24,240
The similar with the other opening, other closing records.

216
00:13:28,160 --> 00:13:35,210
Now, if the opening record is of second kind, this one, then it's open, then at the top of the stack,

217
00:13:35,210 --> 00:13:36,650
I should have this one record.

218
00:13:38,060 --> 00:13:40,160
Astarte, Barback, and similarly.

219
00:13:41,280 --> 00:13:42,450
I should have this one.

220
00:13:45,490 --> 00:13:48,790
OK, so and when the string will finish.

221
00:13:50,610 --> 00:13:56,190
So after outrating over the computer string, so after a complete string, I have to check whether the

222
00:13:56,190 --> 00:13:58,200
string, whether this tank is empty or not.

223
00:13:58,260 --> 00:14:00,910
OK, so at this point, I will check.

224
00:14:01,350 --> 00:14:02,250
So if.

225
00:14:03,210 --> 00:14:04,350
The steak is empty.

226
00:14:07,740 --> 00:14:09,420
Then you can simply return to.

227
00:14:11,960 --> 00:14:17,150
Otherwise, this is not a valid parenthesis we can return false.

228
00:14:17,450 --> 00:14:19,670
OK, so let us run our good.

229
00:14:24,450 --> 00:14:26,100
OK, now let's try to submit.

230
00:14:31,270 --> 00:14:34,130
OK, so our goal is working fine now.

231
00:14:34,420 --> 00:14:37,270
Let us try to understand our code, OK?

232
00:14:43,050 --> 00:14:45,720
So basically, this is our rule number one.

233
00:14:45,780 --> 00:14:52,230
OK, so this was our number one, that if you have opening record of any kind, the nightly push inside

234
00:14:52,230 --> 00:14:52,800
the stack.

235
00:14:52,830 --> 00:14:54,140
OK, so no difficulty.

236
00:14:54,540 --> 00:15:00,230
Now, let us explain this line, why this line is important to right here and otherwise I will get.

237
00:15:00,540 --> 00:15:02,010
So this is runtime error.

238
00:15:02,130 --> 00:15:10,150
OK, now let's say our string is basically it is consisting only of closing brackets.

239
00:15:10,320 --> 00:15:13,920
OK, so I let's it is it consists of only closing.

240
00:15:14,610 --> 00:15:16,540
So what should be our output at this point.

241
00:15:16,560 --> 00:15:18,070
Obviously our output should be false.

242
00:15:18,570 --> 00:15:20,160
This is not a valid string.

243
00:15:20,190 --> 00:15:21,990
OK, valid point is, is that not there.

244
00:15:22,740 --> 00:15:25,350
So now let us try to execute our.

245
00:15:25,590 --> 00:15:26,300
What will happen.

246
00:15:26,610 --> 00:15:27,920
So this is our stack.

247
00:15:29,350 --> 00:15:32,390
I was taken initially and I am standing at this point.

248
00:15:32,960 --> 00:15:35,400
OK, so this condition will be false.

249
00:15:36,010 --> 00:15:37,770
So I will go to the Elzbieta part.

250
00:15:38,140 --> 00:15:40,570
So inside the LS part of this line is not there.

251
00:15:40,600 --> 00:15:45,120
OK, so if this line is not there, then what I will do, I will execute this line.

252
00:15:45,520 --> 00:15:48,430
So if I equals equals closing record.

253
00:15:48,430 --> 00:15:51,060
No, this is not this type of closing.

254
00:15:51,090 --> 00:15:53,060
I could know this type of closing record.

255
00:15:53,100 --> 00:15:53,410
Yes.

256
00:15:53,650 --> 00:15:55,770
So I will execute this part.

257
00:15:55,780 --> 00:15:57,180
Now it is written here.

258
00:15:57,520 --> 00:16:04,120
So if you, if you will see carefully I have written as top ecological opening record.

259
00:16:04,150 --> 00:16:13,970
OK, so I have written as the top inside the if condition as the top opening record of this kind.

260
00:16:14,680 --> 00:16:16,930
So now see, the stack is empty.

261
00:16:18,620 --> 00:16:25,280
Stack is empty and basically what we are trying to do here is you are trying to get the top element

262
00:16:25,280 --> 00:16:29,900
of the stack and the stack is basically empty at this point.

263
00:16:30,170 --> 00:16:31,040
So what will happen?

264
00:16:31,040 --> 00:16:38,330
You will get a random error because you are trying to access the first element of the stack, the topmost

265
00:16:38,450 --> 00:16:41,140
element of the stack and the stack is basically empty.

266
00:16:41,600 --> 00:16:43,550
So that's why you will get married.

267
00:16:43,760 --> 00:16:48,290
So first, accessing so before accessing the top element, you have to read this condition.

268
00:16:49,470 --> 00:16:56,280
OK, before accessing the top element, this condition is mandatory to read, you can update this condition

269
00:16:56,280 --> 00:16:56,660
also.

270
00:16:57,120 --> 00:16:59,010
OK, so what I'm doing here.

271
00:16:59,430 --> 00:17:04,170
So before going inside this elusive part, I'm checking first.

272
00:17:04,950 --> 00:17:05,940
I'm checking first.

273
00:17:05,940 --> 00:17:12,060
If the steak is not empty, then then you perform this operation and the steak is in pretty badly written

274
00:17:12,060 --> 00:17:12,569
defaults.

275
00:17:13,760 --> 00:17:17,950
OK, so it is very important before accessing the topmost element.

276
00:17:18,020 --> 00:17:21,020
First, you have to check whether their stack is empty or not.

277
00:17:21,329 --> 00:17:24,109
OK, because of the stack was the stake is empty.

278
00:17:24,140 --> 00:17:25,160
You will get at it.

279
00:17:25,819 --> 00:17:26,180
OK.

280
00:17:27,230 --> 00:17:30,320
So I'm repeating myself, what is the problem?

281
00:17:30,620 --> 00:17:33,500
So you want to access the top element of the stick?

282
00:17:34,280 --> 00:17:36,140
OK, now there are two cases.

283
00:17:36,560 --> 00:17:40,310
Either the top element exist or the top element doesn't exist.

284
00:17:40,500 --> 00:17:42,960
So if the top element exists, it will work fine.

285
00:17:43,100 --> 00:17:48,040
Now, if the stack is empty, so if the stack is empty, element will not exist.

286
00:17:48,050 --> 00:17:50,460
And at this point you will get married.

287
00:17:51,080 --> 00:17:55,180
So that's why I first you have to check where the stack is empty or not.

288
00:17:55,940 --> 00:17:58,290
So you can also write it like here.

289
00:17:58,580 --> 00:17:59,940
So first I have to check.

290
00:18:00,200 --> 00:18:03,170
So if this tag is not empty.

291
00:18:06,080 --> 00:18:06,650
And.

292
00:18:08,280 --> 00:18:11,260
As top equals equals this opening record.

293
00:18:11,300 --> 00:18:16,680
OK, so you can write like this first, I am checking if the stake is not empty and then I will execute

294
00:18:16,680 --> 00:18:17,250
this part.

295
00:18:17,890 --> 00:18:22,350
OK, so you can also write this and then you can remove this part if you want to.

296
00:18:22,810 --> 00:18:24,630
OK, so how this will work.

297
00:18:26,790 --> 00:18:28,660
So basically what is the meaning of this line?

298
00:18:29,100 --> 00:18:32,580
So this line says if the steak is not empty.

299
00:18:35,280 --> 00:18:39,140
OK, now there are two parts of the state will be empty or it will not be empty.

300
00:18:39,330 --> 00:18:41,190
So if the stack is empty.

301
00:18:42,800 --> 00:18:48,740
Then I will not execute the second part, I will not execute the part after the end.

302
00:18:48,800 --> 00:18:52,330
OK, so this part will not be executed, so we will not get married.

303
00:18:52,340 --> 00:18:57,280
So runtime error will not be there because this part will not be executed.

304
00:18:58,300 --> 00:19:03,400
OK, and if the bag is not empty, so Steg is not empty.

305
00:19:04,820 --> 00:19:10,970
If the steak is not empty, then I will check the second condition and obviously if the steak is not

306
00:19:10,970 --> 00:19:12,510
empty, we will get the top element.

307
00:19:12,710 --> 00:19:14,660
So, again, our goal will work fine.

308
00:19:15,020 --> 00:19:23,810
OK, so instead of writing and the steak is not empty and every condition I decided to write at once

309
00:19:23,810 --> 00:19:24,070
here.

310
00:19:24,260 --> 00:19:31,640
OK, so instead of writing this condition with every condition, with every condition, I decided to

311
00:19:31,640 --> 00:19:34,190
write at once at the starting of the loop.

312
00:19:34,190 --> 00:19:36,230
At the starting of the conditions, ok.

313
00:19:38,940 --> 00:19:41,550
So I hope this court is clear.

314
00:19:42,180 --> 00:19:48,060
OK, so if you have any doubt in this problem, you can ask me sort of the space complexity.

315
00:19:48,090 --> 00:19:50,770
Time is linear and space is also on.

316
00:19:50,820 --> 00:19:52,200
OK, thank you.

