Check for balanced parentheses using stack

  Рет қаралды 478,865

mycodeschool

mycodeschool

Күн бұрын

Пікірлер: 197
@20mymysf
@20mymysf 5 жыл бұрын
I clearly understand this subject now. I saw this for free and am in debt from school for something they couldn't teach. The irony. Thanks for the lovely video!!
@higlights2938
@higlights2938 4 жыл бұрын
H
@poseidon2735
@poseidon2735 4 жыл бұрын
This guy is one enough to make learn coding the whole student community
@selenayyalcn8221
@selenayyalcn8221 2 жыл бұрын
the best data structures video on the internet i've ever seen. thanks for the clarity and understandability even 9 years later. hope you're okey
@neelanshsharma275
@neelanshsharma275 Жыл бұрын
I'm really grateful sir, I will forever remember you. Thank you from the bottom of my heart ❤
@suvansingh3684
@suvansingh3684 5 жыл бұрын
The best explanation of this concept , Last Un-Closed , First Un-Closed , makes it clear why a stack must be used. This detail has been left out it most explanations. Great work. @9:27
@abocidejofa9686
@abocidejofa9686 10 жыл бұрын
No one can explain any clearer that this!!! thank you
@tejasrawat1337
@tejasrawat1337 8 жыл бұрын
your classes are very interactive as programming perspective. I could say this is the best data structure tutorial till now we have in web.
@dephc0n1
@dephc0n1 8 жыл бұрын
Thank you for this explanation. I have to do this exact problem for a job interview coming in the fall.
@rohanbangash5827
@rohanbangash5827 3 жыл бұрын
4 years later, how did it go lol
@dephc0n1
@dephc0n1 3 жыл бұрын
@@rohanbangash5827 Nailed this question on every interview. Funny enough I work for a company that did not ask this question. It's a great algorithm to help think about the benefits of a stack data structure.
@rebelutionarygaming8875
@rebelutionarygaming8875 7 жыл бұрын
I've learned more abuout data structures from your videos, than I have from school. Thank you.
@DineshKumar-lq2ck
@DineshKumar-lq2ck 2 жыл бұрын
Words are short to express my gratitude. I am able to code it in C# with your advised alsogrithm, it works in all conditions. Thank you once again.
@aswin2pranav
@aswin2pranav 8 жыл бұрын
You're a bloody genius... Thanks!
@eklavyaprasad5009
@eklavyaprasad5009 3 жыл бұрын
Remembering Lord Harsha ❤️
@drkato9
@drkato9 8 жыл бұрын
Your classes are all fantastic. Thanks so much!!!
@vedangkavathiya8426
@vedangkavathiya8426 4 жыл бұрын
Sir your explanation is too good thanks for explaining me this topic of stack... 😃
@plsjustinit
@plsjustinit 10 жыл бұрын
Wow, exactly the same as what I did in an Amazon phone interview. I forgot to check s.isEmpty while peeking the stack until it threw exception in one testing, but after adding it back, it is exactly the same as what it shows here. Cool!
@nikunjchapagain5654
@nikunjchapagain5654 7 жыл бұрын
did you get the job tho?
@muhammadbilalmalik7292
@muhammadbilalmalik7292 4 жыл бұрын
@@nikunjchapagain5654 yes he was refrenced by me !!
@parasdesh
@parasdesh 4 жыл бұрын
I’ve been asked this question in coding interview.
@mdkaif-kf7oh
@mdkaif-kf7oh 3 жыл бұрын
You have the best way of teaching..🔥🔥🔥
@branvinivideo
@branvinivideo 20 күн бұрын
Wonderful explanation, thank you!
@hemantyadav1047
@hemantyadav1047 3 жыл бұрын
We can maintain two data structure stack and queue. stack for the opening and queue for the closing parentheses. The we can compare these two if all the parentheses are in the order.
@bhaktitarang04
@bhaktitarang04 4 жыл бұрын
I am really grateful to the person for this awesome compilation of Data Structures Thanks You!
@davidechiappetta
@davidechiappetta 2 жыл бұрын
using a stack seems a bit excessive to me, since for each character to match it must make a call to the C ++ library and this consumes cpu and memory, you can simply use a byte array of fixed length, as large as the number of characters or symbols to compare, in which in each element you can save a number corresponding to the character to be balanced, e.g. 1 corresponds to square brackets and 2 corresponds to round brackets and a global pointer which moves to the right (g_ptr ++) to insert char x,y,z and to move to the left (g_ptr--) to compare and remove char z,y,x , (if the characters do not match or if the pointer goes below zero then there is an unbalance) a method that can also be used to keep track of the states of C directives (for example excluding everything that descends from #else (including other #if #else #endif and resuming the block at the exit of the first #endif) where it is not worth using a stack or a parser e.g. LALR
@ighsight
@ighsight Жыл бұрын
Your comment is overkill, like the whole point was to show you know as much as the presenter. The video is teaching how to use an important data structure, all your extra talk about efficiency and libraries is irrelevant. Developers seeking jobs need to know what a stack is and how to use it. Period. Understanding limitations of data structures and their accompanying algorithms IS important, but there’s no need shoehorn that in to a basic introductory video where folks are just learning how to use the structure.
@Hyperflow
@Hyperflow 3 жыл бұрын
thank you for this input method, made it way easier for me to understand
@kritikashukla9937
@kritikashukla9937 3 жыл бұрын
So smoothly explained. Thank you!
@rhugvedisali9123
@rhugvedisali9123 7 ай бұрын
Easy to understand teaching
@nooreldali7432
@nooreldali7432 5 жыл бұрын
my exam is in two hours you are lifesaver
@indavarapuaneesh2871
@indavarapuaneesh2871 7 жыл бұрын
Excellent marvellous extraordinary mind blowing fantastic awesome.no words to describe your work.thanks guruji
@danielsenik2432
@danielsenik2432 6 жыл бұрын
thanks, this video helped a lot to understand the general idea of the algorithm
@faizannisar1515
@faizannisar1515 7 жыл бұрын
amazing lectures as well as brilliant voice
@prashantbajpai2142
@prashantbajpai2142 11 жыл бұрын
good work sir thnk u so much fr make a awesome creation of videos u r the real teacher thnks a lot sir plz upload more videos related to Tree,Graphs,Constructor,exceptional handling,file handling etc
@brando8045
@brando8045 5 жыл бұрын
excellent video! i understand this concept much better now.
@prashantdiwakar1302
@prashantdiwakar1302 4 ай бұрын
Beautiful Explanation
@lamle5183
@lamle5183 8 жыл бұрын
You make it sooo easy to understand. Thanks a lot!
@GGlearn
@GGlearn Жыл бұрын
thank you a lot . you are a great teacher
@JangBahadur3028
@JangBahadur3028 8 жыл бұрын
Really awesome and helpful explaination. Thanks.
@pramodbhat6314
@pramodbhat6314 3 жыл бұрын
The best explanation.
@raghulg6155
@raghulg6155 4 жыл бұрын
Much smaller code in C: // Check for paranthesis using stack and the stack here is implemented using Linked lists #include #include #include int flag = 0; struct Node{ int data; struct Node *link; }; struct Node *head; void push(int x) { struct Node *nod=(struct Node*)malloc(sizeof(struct Node)); nod->data=x; nod->link=head; head=nod; } char pop() { char n; struct Node *temp=head; if(head==NULL) return; n=temp->data; head=head->link; free(temp); return n; } void checkforparanthesis(char *A,int x) { char c; for(int i=0;i
@hanadotexe
@hanadotexe 4 жыл бұрын
thank u very much, very well explained! helped me a lot.
@Tech2NatureAI
@Tech2NatureAI 4 жыл бұрын
if length of the string is "odd" its invalid , only we should check for the even ones.
@brahmkaransingh8804
@brahmkaransingh8804 4 жыл бұрын
If the length is odd you should simply return false as there is no chance of matching of brackets
@jitenkhatri8001
@jitenkhatri8001 4 жыл бұрын
Happy teacher's day bro
@virajdoshi
@virajdoshi 3 жыл бұрын
Why would you use stack to 'park' the parantheses ? how is the possibility of any bracket finding a match within the 'parked' list/array ? Dosen;t the pop method give you always the topmost (last) instance ?
@ChandraShekhar-by3cd
@ChandraShekhar-by3cd 5 жыл бұрын
Amazing Video..please upload some more videos on Interview Questions and MultiThreading and Design Pattern
@aymardeoolatounde
@aymardeoolatounde 2 жыл бұрын
Hi Sir, I think after set variable n with expression length, we can check if n is even. If it’s not, n is odd and return false (Expression will contain only brackets).
@rpitpatel1004
@rpitpatel1004 5 жыл бұрын
Amazing explanation Thank you sir
@Mohit-nw5jr
@Mohit-nw5jr 7 жыл бұрын
you r the god of data structures!!! thanks a lot! BTW where are u from in India??
@liam190
@liam190 7 жыл бұрын
It definitely seems like it. Indians are generally very good at programming anyways.
@kyssl
@kyssl 7 жыл бұрын
Liam he's from India and he died in 2015
@goodToBeLost
@goodToBeLost 7 жыл бұрын
Sid He died in 2015? You mean him or the person he founded My Code School with, Harsha Suryanarayana? He's the one who unfortunately passed away in 2014 or so right? This person is Animesh Nayan and he is pretty much alive?
@kyssl
@kyssl 7 жыл бұрын
Amrita Basu i was talking abt Harsha
@goodToBeLost
@goodToBeLost 7 жыл бұрын
Sid So Harsha created these videos ? I was under the impression that it was Animesh.
@subforsubcommunitycenter6961
@subforsubcommunitycenter6961 4 жыл бұрын
Wow u have all the thing i searching for
@raedkhader6263
@raedkhader6263 8 жыл бұрын
I cant thank you enough, so simple and clear ;)
@Raven_SG
@Raven_SG 6 жыл бұрын
Thanks for the help! really help me alot in my studies! !
@maycodes
@maycodes 2 жыл бұрын
Thank you , what a great contribution.
@samarthkhatwani
@samarthkhatwani 6 жыл бұрын
hats off bro... thank you so much for this amazing series
@aakashmudigonda3375
@aakashmudigonda3375 4 жыл бұрын
if anyone requires the "C" code for the above program :: #include #include #include #define MAX 100 struct Node{ int data; struct Node* next; }; typedef struct Node node; node* top = NULL; // PUSH() void Push(char c) { node* temp = (node*)malloc(sizeof(node)); temp->data = c; temp->next = top; top = temp; } // POP() void Pop() { node* temp; if (top == NULL) { return; } temp = top; top = top->next; free(temp); } int isMatching(char c){ if ( top->data == '(' && c == ')' ){ return 1; } else if ( top->data == '[' && c == ']' ){ return 1; } else if ( top->data == '{' && c == '}' ){ return 1; } else return 0; } void isBalanced(char exp[], unsigned long len){ for (int i = 0; i
@dharshanaapiskala7558
@dharshanaapiskala7558 2 жыл бұрын
TQ so much !!!!
@rownitakhanam5013
@rownitakhanam5013 5 жыл бұрын
By mercy of Allah,you 're saving careers of a lot of people.
@cyberpsybin
@cyberpsybin 5 жыл бұрын
stfu
@sagarmittal9998
@sagarmittal9998 4 жыл бұрын
Nice explanation👍👍
@pachavamanasa5833
@pachavamanasa5833 7 жыл бұрын
thanku you so much sir you are healping us a lot
@kmahesh-eq4ji
@kmahesh-eq4ji 4 жыл бұрын
the given code in the git hub will fail for the test case when we include the space in the user input because the taking of string input will not execute after a blank space I'm still having doubt on the concepts of strings and characters of user input oriented.. i will also do silly mistakes like this code which was uploaded on git hub
@rinjakundu
@rinjakundu 5 жыл бұрын
Nicely explained! Really helpful
@bhawnakukreja7589
@bhawnakukreja7589 7 жыл бұрын
great explanation sir...thanks a lot
@santgupta357
@santgupta357 10 жыл бұрын
Nice videos. To the point and easy to understand. Where can I get code if I want ?
@mayursatbhai1624
@mayursatbhai1624 9 жыл бұрын
Sant Gupta open "see more" below the video and click on "see source code here.
@sbk1398
@sbk1398 7 жыл бұрын
excellent explanation
@kritikaushal7487
@kritikaushal7487 6 жыл бұрын
Great explanation !
@shashwatsingh2793
@shashwatsingh2793 5 жыл бұрын
Great video sir
@diksha5746
@diksha5746 7 жыл бұрын
Thank you. It was very helpful
@aymensekhri2133
@aymensekhri2133 2 жыл бұрын
Thanks a lot, very helpful
@yassinedownpourz
@yassinedownpourz 2 жыл бұрын
// C code Implementation: #include #include #include #include typedef struct stack{ char data; struct stack *next; } stack; stack *push(stack *top, char chart){ stack *new = malloc(sizeof *new); new->data = chart; new->next = top; return new; } stack *pop(stack *top){ if(!top) return NULL; stack *temp = top; top = top->next; free(temp); return top; } char Stack_top(stack *top){ if(!top) return '\0'; return top->data; } bool isEmpty(stack *top){ return !top ? true : false; } bool isPair(char op, char ed){ if(op == '(' && ed == ')') return true; if(op == '[' && ed == ']') return true; if(op == '{' && ed == '}') return true; return false; } bool balanced_parantheses(char str[]){ stack *tmp = NULL; for(size_t i = 0; i < strlen(str); i++) { if(str[i] == '(' || str[i] == '{' || str[i] == '[') tmp = push(tmp, str[i]); else if(str[i] == ')' || str[i] == '}' || str[i] == ']') { if(!isEmpty(tmp) && isPair(Stack_top(tmp), str[i])) tmp = pop(tmp); else return false; } } return isEmpty(tmp) ? true : false; } int main(void){ char str[] = "{()((((("; balanced_parantheses(str) ? printf("Balanced") : printf("NOT Balanced"); }
@kasyapdharanikota8570
@kasyapdharanikota8570 3 жыл бұрын
best explanation
@chanduseshadri
@chanduseshadri 10 жыл бұрын
I would add this piece of code, if(expression.length() % 2 != 0){ return false; } in the beginning, anyway good video :)
@chanduseshadri
@chanduseshadri 8 жыл бұрын
+Prem Kumar Hi Prem, Thanks for the answer. That piece of code I wanted to add would only fit if the expression contains no other characters except parentheses (could be any closing ones "''" too). Since the video was only mentioning a string with parentheses, I considered that would fit in there. Yes, if the string can include all sorts of characters, as you said that won't work. Thanks for taking your time anyway :)
@kanishkjain2767
@kanishkjain2767 7 жыл бұрын
nice logic it will reduce the time and space complexity for odd cases to a great extend
@rohanmehto7609
@rohanmehto7609 7 жыл бұрын
when in use, we dont check for expressions which only contain paranthesis (thats stupid) , so the expression with odd length can have balanced pararenthesis.
@GamjaField
@GamjaField 8 жыл бұрын
How do I deal with the brackets inside the quote, or comments?
@webdevschool6597
@webdevschool6597 5 жыл бұрын
best of data structures
@p16-l1y
@p16-l1y 4 жыл бұрын
Thank you this really helped! c:
@u2lover10
@u2lover10 5 жыл бұрын
pseudo code rocks! Thanks!
@saurav4180
@saurav4180 5 жыл бұрын
great job sir....
@tharugeethz6021
@tharugeethz6021 9 жыл бұрын
Thank u so much! It was really helpful !
@naveedachkal9734
@naveedachkal9734 9 жыл бұрын
helpfull should have c version also
@Kalpeshsoni
@Kalpeshsoni 4 жыл бұрын
gist.github.com/mycodeschool/7207410
@chayankanungo4778
@chayankanungo4778 4 жыл бұрын
Thank you so much Sir!
@Skrafutae
@Skrafutae 6 жыл бұрын
Nicely explained :)
@YogaandLivingwithChai
@YogaandLivingwithChai 8 жыл бұрын
Can you solve for String Anagram and word ladder?
@manassrivastava6452
@manassrivastava6452 4 жыл бұрын
This reminds me of Push Down Automata(PDA)
@ibrahimshaikh3642
@ibrahimshaikh3642 5 жыл бұрын
Many many thanks,keep it up
@southpeng
@southpeng 11 жыл бұрын
Very clear instruction!
@seraj_valley
@seraj_valley 4 жыл бұрын
u r legend sir
@mdsolaimanchowdhury627
@mdsolaimanchowdhury627 9 жыл бұрын
It was really helpful !
@nands4410
@nands4410 7 жыл бұрын
What are some more popular programming interview questions like this one?
@animefreak0514
@animefreak0514 7 жыл бұрын
In the about section of his channel he has a link to an article about programing interviews.
@nands4410
@nands4410 7 жыл бұрын
Christian Jusino that too is outdated but
@csanchezcuba
@csanchezcuba 9 жыл бұрын
can someone tell me how to aproach this problem using recursion instead of stacks? the signature of my method should be public boolean isBalanced(String in)
@trishachander1602
@trishachander1602 4 жыл бұрын
what's the algorithm to check that the top doesn't pair with i?
@maidul13
@maidul13 5 жыл бұрын
would this o(n^2) time complexity? because you have for loop and inside you append items which take O(n)?
5 жыл бұрын
Append, Pop, Top, IsEmpty operations are O(1) in Stack. Even if we pop or append all the chars to stack, we would do that n times.
@rawan8432
@rawan8432 10 жыл бұрын
so clear , thanks alot
@alicedinh3710
@alicedinh3710 9 жыл бұрын
What is about parentheses in comments? How to ignore them?
@MB-sk6oc
@MB-sk6oc 6 жыл бұрын
It was Helpful :)
@Arundevvb1
@Arundevvb1 Жыл бұрын
Thank you.
@memofahood4543
@memofahood4543 10 жыл бұрын
Amazing explanation :)
@FaisalKhan-ce2qu
@FaisalKhan-ce2qu 4 жыл бұрын
Your code uploaded on github is wrong and i cant find the mistake and it got annoying! Thanks!
@nikhilmaheshwari6576
@nikhilmaheshwari6576 4 жыл бұрын
Sir My program terminates when stack is empty and char comes as ')' or ']' or '}' it shows segmentation error
@nikhilmaheshwari6576
@nikhilmaheshwari6576 4 жыл бұрын
Here's my code #include #include #include using namespace std; void balanced(string array) { int n = array.length(); stack para; for(int i=0;i
@kungschannel1179
@kungschannel1179 8 жыл бұрын
when i input the expression 1 - ) 2 - 3 ( , it still return "Balance" . What happen here, guy ?
@jamesabasifreke
@jamesabasifreke 8 жыл бұрын
Do you mind sharing your code? I can help you debug
@imaadnaeemansari8693
@imaadnaeemansari8693 3 жыл бұрын
thank you sir!
@kcmag1k
@kcmag1k 9 жыл бұрын
superb!!
@bradenmills2711
@bradenmills2711 4 жыл бұрын
what would the Big0 of this be?
@rajdeepkhandelwal1653
@rajdeepkhandelwal1653 4 жыл бұрын
thank you 🙌
@usama57926
@usama57926 6 жыл бұрын
thank u brother
@shahriarbadhon4596
@shahriarbadhon4596 4 жыл бұрын
Just awesome
@ralphlaurennn
@ralphlaurennn 3 жыл бұрын
Thank youuu!!!
@aarb17
@aarb17 10 жыл бұрын
Great, thank you
Infix to Postfix using stack
18:20
mycodeschool
Рет қаралды 756 М.
Reverse a string or linked list using stack.
16:25
mycodeschool
Рет қаралды 386 М.
Perfect Pitch Challenge? Easy! 🎤😎| Free Fire Official
00:13
Garena Free Fire Global
Рет қаралды 100 МЛН
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 167 МЛН
L2. Check for Balanced Parentheses | Stack and Queue
12:28
take U forward
Рет қаралды 45 М.
Check if a binary tree is binary search tree or not
16:30
mycodeschool
Рет қаралды 380 М.
EP06 - Python Stack - Balanced Parentheses Checker
7:52
Coding with Ashwin
Рет қаралды 13 М.
Evaluation of Prefix and Postfix expressions using stack
14:10
mycodeschool
Рет қаралды 636 М.
Valid Parentheses | Leetcode
12:48
take U forward
Рет қаралды 201 М.
How Hot is Light? How Lasers Bend the Rules of Heat Transfer
8:07
The Action Lab
Рет қаралды 153 М.
you will never ask about pointers again after watching this video
8:03
What is the i really doing in Schrödinger's equation?
25:06
Welch Labs
Рет қаралды 38 М.