Check for balanced parentheses using stack

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ต.ค. 2013
  • See complete series on data structures here:
    • Data structures
    Algorithm or program to check for balanced parentheses in an expression using stack data structure. This is a popular programming interview question.
    See source code here:
    gist.github.com/mycodeschool/...
    For practice problems and more, visit: www.mycodeschool.com
    Like us on Facebook: / mycodeschool
    Follow us on twitter: / mycodeschool

ความคิดเห็น • 193

  • @20mymysf
    @20mymysf 5 ปีที่แล้ว +145

    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!!

  • @abocidejofa9686
    @abocidejofa9686 10 ปีที่แล้ว +28

    No one can explain any clearer that this!!! thank you

  • @selenayyalcn8221
    @selenayyalcn8221 2 ปีที่แล้ว +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

  • @suvansingh3684
    @suvansingh3684 5 ปีที่แล้ว +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

  • @drkato9
    @drkato9 8 ปีที่แล้ว +5

    Your classes are all fantastic. Thanks so much!!!

  • @tejasrawat1337
    @tejasrawat1337 8 ปีที่แล้ว +13

    your classes are very interactive as programming perspective. I could say this is the best data structure tutorial till now we have in web.

  • @aswin2pranav
    @aswin2pranav 7 ปีที่แล้ว +62

    You're a bloody genius... Thanks!

  • @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.

  • @rebelutionarygaming8875
    @rebelutionarygaming8875 6 ปีที่แล้ว +14

    I've learned more abuout data structures from your videos, than I have from school. Thank you.

  • @dephc0n1
    @dephc0n1 8 ปีที่แล้ว +10

    Thank you for this explanation. I have to do this exact problem for a job interview coming in the fall.

    • @rohanbangash5827
      @rohanbangash5827 3 ปีที่แล้ว +3

      4 years later, how did it go lol

    • @dephc0n1
      @dephc0n1 3 ปีที่แล้ว +5

      @@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.

  • @lamle5183
    @lamle5183 8 ปีที่แล้ว

    You make it sooo easy to understand. Thanks a lot!

  • @neelanshsharma275
    @neelanshsharma275 10 หลายเดือนก่อน +1

    I'm really grateful sir, I will forever remember you.
    Thank you from the bottom of my heart ❤

  • @kritikashukla9937
    @kritikashukla9937 3 ปีที่แล้ว

    So smoothly explained. Thank you!

  • @eklavyaprasad5009
    @eklavyaprasad5009 3 ปีที่แล้ว +9

    Remembering Lord Harsha ❤️

  • @JangBahadur3028
    @JangBahadur3028 7 ปีที่แล้ว +2

    Really awesome and helpful explaination. Thanks.

  • @bhaktitarang04
    @bhaktitarang04 4 ปีที่แล้ว

    I am really grateful to the person for this awesome compilation of Data Structures Thanks You!

  • @faizannisar1515
    @faizannisar1515 7 ปีที่แล้ว +1

    amazing lectures as well as brilliant voice

  • @Hyperflow
    @Hyperflow 3 ปีที่แล้ว

    thank you for this input method, made it way easier for me to understand

  • @poseidon2735
    @poseidon2735 4 ปีที่แล้ว +46

    This guy is one enough to make learn coding the whole student community

  • @danielsenik2432
    @danielsenik2432 5 ปีที่แล้ว

    thanks, this video helped a lot to understand the general idea of the algorithm

  • @raedkhader6263
    @raedkhader6263 8 ปีที่แล้ว +2

    I cant thank you enough, so simple and clear ;)

  • @vedangkavathiya8426
    @vedangkavathiya8426 4 ปีที่แล้ว +1

    Sir your explanation is too good thanks for explaining me this topic of stack... 😃

  • @samarthkhatwani
    @samarthkhatwani 5 ปีที่แล้ว

    hats off bro... thank you so much for this amazing series

  • @mdkaif-kf7oh
    @mdkaif-kf7oh 3 ปีที่แล้ว

    You have the best way of teaching..🔥🔥🔥

  • @indavarapuaneesh2871
    @indavarapuaneesh2871 6 ปีที่แล้ว

    Excellent marvellous extraordinary mind blowing fantastic awesome.no words to describe your work.thanks guruji

  • @Raven_SG
    @Raven_SG 5 ปีที่แล้ว

    Thanks for the help! really help me alot in my studies! !

  • @rinjakundu
    @rinjakundu 5 ปีที่แล้ว

    Nicely explained!
    Really helpful

  • @brando8045
    @brando8045 4 ปีที่แล้ว

    excellent video! i understand this concept much better now.

  • @prashantbajpai2142
    @prashantbajpai2142 10 ปีที่แล้ว +2

    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

  • @hanadotexe
    @hanadotexe 3 ปีที่แล้ว +1

    thank u very much, very well explained! helped me a lot.

  • @kritikaushal7487
    @kritikaushal7487 5 ปีที่แล้ว

    Great explanation !

  • @southpeng
    @southpeng 10 ปีที่แล้ว

    Very clear instruction!

  • @pachavamanasa5833
    @pachavamanasa5833 7 ปีที่แล้ว

    thanku you so much sir
    you are healping us a lot

  • @diksha5746
    @diksha5746 7 ปีที่แล้ว

    Thank you. It was very helpful

  • @bhawnakukreja7589
    @bhawnakukreja7589 7 ปีที่แล้ว

    great explanation sir...thanks a lot

  • @rpitpatel1004
    @rpitpatel1004 4 ปีที่แล้ว

    Amazing explanation Thank you sir

  • @tharugeethz6021
    @tharugeethz6021 9 ปีที่แล้ว

    Thank u so much! It was really helpful !

  • @maycodes
    @maycodes 2 ปีที่แล้ว

    Thank you , what a great contribution.

  • @sbk1398
    @sbk1398 7 ปีที่แล้ว

    excellent explanation

  • @hemantyadav1047
    @hemantyadav1047 2 ปีที่แล้ว

    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.

  • @GGlearn
    @GGlearn 10 หลายเดือนก่อน

    thank you a lot . you are a great teacher

  • @mdsolaimanchowdhury627
    @mdsolaimanchowdhury627 8 ปีที่แล้ว

    It was really helpful !

  • @rhugvedisali9123
    @rhugvedisali9123 3 หลายเดือนก่อน +1

    Easy to understand teaching

  • @pramodbhat6314
    @pramodbhat6314 2 ปีที่แล้ว

    The best explanation.

  • @u2lover10
    @u2lover10 5 ปีที่แล้ว

    pseudo code rocks! Thanks!

  • @rawan8432
    @rawan8432 10 ปีที่แล้ว

    so clear , thanks alot

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd 5 ปีที่แล้ว

    Amazing Video..please upload some more videos on Interview Questions and MultiThreading and Design Pattern

  • @Skrafutae
    @Skrafutae 5 ปีที่แล้ว

    Nicely explained :)

  • @user-ox1dr3ec7m
    @user-ox1dr3ec7m 4 ปีที่แล้ว +2

    Thank you this really helped! c:

  • @chayankanungo4778
    @chayankanungo4778 3 ปีที่แล้ว

    Thank you so much Sir!

  • @saurav4180
    @saurav4180 5 ปีที่แล้ว

    great job sir....

  • @subforsubcommunitycenter6961
    @subforsubcommunitycenter6961 3 ปีที่แล้ว

    Wow u have all the thing i searching for

  • @ibrahimshaikh3642
    @ibrahimshaikh3642 4 ปีที่แล้ว

    Many many thanks,keep it up

  • @aymensekhri2133
    @aymensekhri2133 2 ปีที่แล้ว

    Thanks a lot, very helpful

  • @aarb17
    @aarb17 10 ปีที่แล้ว

    Great, thank you

  • @parasdesh
    @parasdesh 3 ปีที่แล้ว +3

    I’ve been asked this question in coding interview.

  • @shashwatsingh2793
    @shashwatsingh2793 4 ปีที่แล้ว

    Great video sir

  • @usama57926
    @usama57926 6 ปีที่แล้ว +1

    thank u brother

  • @k.kirankumar2153
    @k.kirankumar2153 7 ปีที่แล้ว

    Thank you

  • @daoanhtuandao8106
    @daoanhtuandao8106 7 ปีที่แล้ว

    thank you so much

  • @slightlygruff
    @slightlygruff ปีที่แล้ว

    Thanks so much!

  • @Arundevvb1
    @Arundevvb1 ปีที่แล้ว

    Thank you.

  • @sagarmittal9998
    @sagarmittal9998 3 ปีที่แล้ว

    Nice explanation👍👍

  • @xof8256
    @xof8256 5 ปีที่แล้ว

    Thank You

  • @MB-sk6oc
    @MB-sk6oc 6 ปีที่แล้ว

    It was Helpful :)

  • @kcmag1k
    @kcmag1k 9 ปีที่แล้ว +1

    superb!!

  • @rajdeepkhandelwal1653
    @rajdeepkhandelwal1653 4 ปีที่แล้ว

    thank you 🙌

  • @plsjustinit
    @plsjustinit 9 ปีที่แล้ว +16

    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!

  • @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

  • @nooreldali7432
    @nooreldali7432 5 ปีที่แล้ว

    my exam is in two hours you are lifesaver

  • @imaadnaeemansari8693
    @imaadnaeemansari8693 3 ปีที่แล้ว

    thank you sir!

  • @kasyapdharanikota8570
    @kasyapdharanikota8570 2 ปีที่แล้ว

    best explanation

  • @davidechiappetta
    @davidechiappetta 2 ปีที่แล้ว +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 9 หลายเดือนก่อน

      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.

  • @seraj_valley
    @seraj_valley 4 ปีที่แล้ว

    u r legend sir

  • @Abhishekjha-rr2nm
    @Abhishekjha-rr2nm 7 ปีที่แล้ว

    thanks

  • @shahriarbadhon4596
    @shahriarbadhon4596 3 ปีที่แล้ว

    Just awesome

  • @chennasreenu4723
    @chennasreenu4723 4 ปีที่แล้ว

    best of data structures

  • @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)

  • @aymardeoolatounde
    @aymardeoolatounde ปีที่แล้ว

    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).

  • @ralphmente3988
    @ralphmente3988 3 ปีที่แล้ว

    Thank youuu!!!

  • @govindprajapati5524
    @govindprajapati5524 5 ปีที่แล้ว

    thanks bro

  • @xahidsakib8346
    @xahidsakib8346 5 ปีที่แล้ว

    HELPFUL

  • @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 ?

  • @santgupta357
    @santgupta357 9 ปีที่แล้ว

    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.

  • @aakashmudigonda3375
    @aakashmudigonda3375 3 ปีที่แล้ว +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

  • @daswarudo1957
    @daswarudo1957 2 ปีที่แล้ว

    thanks!

  • @memofahood4543
    @memofahood4543 10 ปีที่แล้ว

    Amazing explanation :)

  • @YogaandLivingwithChai
    @YogaandLivingwithChai 8 ปีที่แล้ว

    Can you solve for String Anagram and word ladder?

  • @aryangupta560
    @aryangupta560 7 ปีที่แล้ว

    awesome

  • @user-pp6fi2bt4w
    @user-pp6fi2bt4w 4 ปีที่แล้ว

    best one

  • @GamjaField
    @GamjaField 8 ปีที่แล้ว

    How do I deal with the brackets inside the quote, or comments?

  • @kmahesh-eq4ji
    @kmahesh-eq4ji 3 ปีที่แล้ว

    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

  • @Mohit-nw5jr
    @Mohit-nw5jr 7 ปีที่แล้ว +3

    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 6 ปีที่แล้ว +1

      Liam he's from India and he died in 2015

    • @goodToBeLost
      @goodToBeLost 6 ปีที่แล้ว +1

      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 6 ปีที่แล้ว

      Amrita Basu i was talking abt Harsha

    • @goodToBeLost
      @goodToBeLost 6 ปีที่แล้ว +1

      Sid So Harsha created these videos ? I was under the impression that it was Animesh.

  • @IndieTech1
    @IndieTech1 5 ปีที่แล้ว

    NICE really

  • @clintonzhang2941
    @clintonzhang2941 7 ปีที่แล้ว

    Thanks m8

  • @liam190
    @liam190 7 ปีที่แล้ว

    Legend

  • @manassrivastava6452
    @manassrivastava6452 4 ปีที่แล้ว

    This reminds me of Push Down Automata(PDA)

  • @maidul13
    @maidul13 5 ปีที่แล้ว

    would this o(n^2) time complexity? because you have for loop and inside you append items which take O(n)?

    •  4 ปีที่แล้ว

      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.

  • @khaltoumach1243
    @khaltoumach1243 ปีที่แล้ว

    What does POP () function do

  • @jitenkhatri8001
    @jitenkhatri8001 3 ปีที่แล้ว

    Happy teacher's day bro