00:00I have made a class. I have made a main function. I have defined a string in the main function.
00:05This string is in this condition. I should have true.
00:09First, let's see a true case.
00:11I will call public static boolean.
00:14I will name is valid.
00:17I will check whether my string of parentheses is valid or not.
00:23We will check for str. First, let's make a stack.
00:26This time I am making a stack of characters.
00:28Not integers.
00:30s is equal to new stack.
00:33Now I will run a loop.
00:35for int i equals 0 i less than str.length i++.
00:41When will I run the loop?
00:42I will not cover my string length.
00:45First, let's remove character.
00:47character is str.char at i.
00:50This character is my bracket store.
00:52It can be opening and closing.
00:54It means my parenthesis store.
00:56First, let's check the opening condition.
00:59In the opening condition,
01:00if my ch is equal to this,
01:04or my ch is equal to this brace,
01:10or my ch is equal to my square bracket.
01:15I have written all these in the opening case.
01:18This is my opening case.
01:22What do I have to do?
01:23I have to do a stack.
01:25I will push it.
01:26I will push it.
01:27I will push my character ch.
01:28Now let's take else case.
01:30Else case is my closing case.
01:33In closing case,
01:34I will check this first.
01:36First, I will check this.
01:37Is my stack empty.
01:38I mean,
01:39if I have such a string.
01:41I have only closing.
01:43So, for closing.
01:45In closing,
01:46I will also have an opening bracket.
01:48I will not have an opening bracket.
01:50Because my stack is empty.
01:52If there is no opening,
01:53then there is no push.
01:54So, my stack is empty.
01:56So, when I check this closing,
01:57then my stack is empty.
01:59So, what can I say?
02:00This is already invalid string.
02:01In which I have not opened,
02:03then there is a closing case.
02:04In that case,
02:05the stack is empty.
02:06And when the stack is empty,
02:07I will return false.
02:08So, first,
02:09I will check this condition.
02:10If my stack is empty,
02:14then I will return false.
02:18Then, I will check what is my pair form.
02:21How will I check my pair condition?
02:24I will check for that.
02:25I will check that.
02:28If I have this closing,
02:30then I have this closing.
02:32And what is my stack top?
02:33What is my stack top?
02:36If I have this opening.
02:40And.
02:41If I have this opening on the stack,
02:43and this closing in the character,
02:46then a pair will form.
02:48So, I have created condition 1.
02:50This is my condition 1.
02:53This is my condition 1.
02:55Besides condition 2,
02:57or then.
02:59I will write condition 2 in the next line.
03:02If I have curly brace on the stack,
03:04I have curly brace.
03:07And the closing one is also curly brace.
03:11This is condition 2.
03:14Now I will write condition 3.
03:19If I have a square bracket on the stack,
03:23I have a square bracket.
03:24And,
03:25I have a character now.
03:27That is also closed in the square bracket.
03:29So, in this case,
03:29the condition will also be true.
03:32i have a pair.
03:32So,
03:32either it is a pair form or this,
03:35or it is a pair form.
03:35or this can be a pair form.
03:37This pair form means.
03:40This pair is becoming a pair.
03:41Some sort of parenthesis pair.
03:43This pair form means.
03:44This one is becoming a pair.
03:45And this one is becoming a pair form.
03:46This one is becoming a pair.
03:47When a successful pair form,
03:49In that case,
03:50I have to give them a stack to the drag.
03:52and when it is not successful pair form then I will return false here directly here
03:57if it is not successful pair form, it means that there is no problem or invalid thing in my string
04:03so in that case I have returned false
04:05so this loop will continue to work for all my characters
04:11as the loop will be finished
04:13this will be my total else condition
04:14this will be my opening
04:16what I will check in the last one
04:18I will check in the last one
04:19if my stack is empty
04:22in that case I will return true
04:26but not
04:28if the start of the stack is missing
04:32what will remain in my opening brackets
04:34or opening parentheses
04:35in that case I will return false
04:38let's do this
04:38save
04:39now I will call the string
04:45I will call
04:45is valid for my string
04:47let's do this
04:48save and now run
04:48I need to return true
04:52so I have true in this string
04:53and in this string
04:54I have a little problem
04:55or in the beginning
04:57I will add an extra parenthesis
04:58in opening
04:59so in that case I will return false
05:01and if I close it here
05:03then the true return again
05:05it will return true
05:07if I just send this here
05:10I will send this
05:11so in this case we need true return
05:13so in this case I will come in
05:16this case I will have true return
05:17and in this case
05:19if I make this
05:20I will do so
05:22then the true return
05:22so here I will make true
05:24so in this case I will also
05:25as well
05:25our valid parenthesis code
05:27because what I have done
05:29I have done
05:30only one loop
05:31and if the stack
05:33has pushed the이다
05:34and put the operations
05:34again
05:36one time the element
05:37is pushed under
05:38and once the element is pushed out of the stack,
05:40so here I have taken the time complexity
05:43that is big of n.
Comments