Valid Parentheses (LeetCode 20) | Full solution with visuals and animations | Stack Data Structure

Sdílet
Vložit
  • čas přidán 26. 07. 2024
  • Actual problem on LeetCode: leetcode.com/problems/valid-p...
    Chapters:
    00:00 - Intro
    00:52 - Problem Statement
    03:08 - Understand valid parentheses
    06:43 - Using stacks for efficiency
    11:21 - Dry-run of Code
    13:45 - Final Thoughts
    📚 Links to topics I talk about in the video:
    Stack Data Structure: • Stack Data Structure e...
    Other problems on Strings: • Trees
    📘 A text based explanation is available at: studyalgorithms.com
    Code on Github: github.com/nikoo28/java-solut...
    Test-cases on Github: github.com/nikoo28/java-solut...
    📖 Reference Books:
    Starting Learn to Code: amzn.to/3sJm8Wl
    Favorite book to understand algorithms: amzn.to/4848xJH
    Favorite book for data structures: amzn.to/3P96YBv
    Get started for interview preparation: amzn.to/44Nn5du
    🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
    🎥 My Recording Gear:
    Recording Light: amzn.to/3PdsViT
    Microphone: amzn.to/3Exv83x
    Recording Camera: amzn.to/3PwyN8e
    Tablet to sketch and draw: amzn.to/3ZdKVy7
    Sketching Tool: amzn.to/45XJEgY
    Laptop to edit videos: amzn.to/460ofDu
    💻 Get Social 💻
    Follow on Facebook at: / studyalgos
    Subscribe to RSS feeds: studyalgorithms.com/feed/
    Join fan mail: eepurl.com/g9Dadv
    #leetcode #programming #interview

Komentáře • 53

  • @arslanmuhammad4190
    @arslanmuhammad4190 Před 9 měsíci +18

    Love You Sir, Got A Internship. 6 months of struggle. Got Pass my DSA test. Thanks.

    • @nikoo28
      @nikoo28  Před 8 měsíci +4

      all the very best...congrats 😄

    • @adityasingh2883
      @adityasingh2883 Před 2 měsíci +1

      Bhai mujhe guide kardo yaar konsi company main internship mili or kaise mili kya criteria tha?

  • @priyabratapadhi2421
    @priyabratapadhi2421 Před 2 měsíci +1

    thanks,sir, really loved your CLEAR STEP by step explanations.

  • @rawat7203
    @rawat7203 Před 7 měsíci +2

    Keep up the Great work Sir, Thank you

  • @sarthakmahadik9760
    @sarthakmahadik9760 Před 8 měsíci +1

    Very informative . Love you Man

  • @ishimwezachee2179
    @ishimwezachee2179 Před 4 měsíci +2

    You are the best for sure

  • @arslanmuhammad4190
    @arslanmuhammad4190 Před 8 měsíci +1

    By Watching Your Videos now. I solved problems in efficient complexity. Thanks.

  • @nandanihada4461
    @nandanihada4461 Před 13 dny

    great explanation😄

  • @appikeeru5785
    @appikeeru5785 Před 13 dny

    Sir very clear explanation 👌

  • @FR0ZING
    @FR0ZING Před 2 měsíci +1

    thank you so much 🙏🙏🙏

  • @user-ei3ng5kt4n
    @user-ei3ng5kt4n Před 4 měsíci +1

    Thank you sir. im confused from 4 days

  • @user-dg4zz8om1g
    @user-dg4zz8om1g Před 8 měsíci +2

    Amazing Bro . keep it up💘.

  • @subee128
    @subee128 Před 6 měsíci +1

    Thanks

  • @jashanjotbedi1796
    @jashanjotbedi1796 Před 5 měsíci

    Explaination is at par level .

  • @mahalasakini5045
    @mahalasakini5045 Před 8 měsíci

    Sir please make some video on design patterns …it will be helpful… and thanks for wonderful videos on DSA

    • @nikoo28
      @nikoo28  Před 8 měsíci +2

      design patterns, system design, graphs...these are the next series of videos in my pipeline..stay tuned!!

  • @user-ve6kg3cg7b
    @user-ve6kg3cg7b Před 8 měsíci

    Thank you! You’re awesome!
    Can you please also upload a solution of Leetcode 200?

    • @nikoo28
      @nikoo28  Před 8 měsíci +1

      yes, that is a very popular problem indeed. Will make a video soon

  • @Nexgenstory
    @Nexgenstory Před 9 měsíci +1

    Very informative video… could you please make a DSA playlist from a scratch

    • @nikoo28
      @nikoo28  Před 8 měsíci +1

      what kind of videos are you looking for? I have videos on stacks/queue, other data structures...

  • @parthmodi2028
    @parthmodi2028 Před 6 měsíci

    Greak Work Sir

    • @nikoo28
      @nikoo28  Před 6 měsíci +1

      Thanks

    • @MrShrane
      @MrShrane Před měsícem

      @@nikoo28 Sir please answer my one doubt , are we not required to return true?? why was there no returning of true? else how will we get it

    • @MrShrane
      @MrShrane Před měsícem

      ohh got it ,thanks sir understood the last part

  • @tasniatahsin8637
    @tasniatahsin8637 Před 7 měsíci +2

    hi nikhil, there is a line else which made me confused,
    if(is.empty || stack.pop()!=c) why did you write the condition is.empty?? wont it be !is.empty() since if its empty, we are gonna be returning true??

    • @nikoo28
      @nikoo28  Před 7 měsíci

      We are doing this to check if the stack becomes empty without matching all the brackets.

    • @rohithbhandari7836
      @rohithbhandari7836 Před 5 měsíci

      ​@@nikoo28 That is only possible when input contains numbers or letters Is not input always contains parenthesis or brackets ?

    • @nikoo28
      @nikoo28  Před 5 měsíci

      Check the problem constraints

  • @pinnapureddynithinreddy4805
    @pinnapureddynithinreddy4805 Před 8 měsíci +1

    sir can you do videos on solving contest problems every week

    • @nikoo28
      @nikoo28  Před 7 měsíci

      I post a new video every week :)

  • @arslanmuhammad4190
    @arslanmuhammad4190 Před 9 měsíci

    Sir, Which Tree can I learn. I Know about BST and AVL. Can You guide me for More Trees.

    • @abhi_coder6
      @abhi_coder6 Před 9 měsíci +2

      Bhai kafi h yaar bas rulayega kya itna hi karle aur question practice kar bas aur kuch nhi mostly tujhe bst pe bt pe bhi sawal milega avl rare h

    • @nikoo28
      @nikoo28  Před 8 měsíci +3

      exactly, you don't need so much. BST and binary trees are usually enough.

    • @arslanmuhammad4190
      @arslanmuhammad4190 Před 8 měsíci +1

      sir Please I know about BST and AVL but What are other Binary trees.

    • @arslanmuhammad4190
      @arslanmuhammad4190 Před 8 měsíci

      @@abhi_coder6 yaar to sai khta ha lekin smj nai a rai or konsay binary trees.

    • @nikoo28
      @nikoo28  Před 8 měsíci +1

      @@arslanmuhammad4190 solve more problems on binary trees, just understanding the concept is not enough. Try to find the lowest common ancestor, least paths, backtracking in trees etc.

  • @arslanmuhammad4190
    @arslanmuhammad4190 Před 8 měsíci +1

    sir When You are going to work on Graphs.

    • @nikoo28
      @nikoo28  Před 8 měsíci +2

      very soon. My next week video will be on introduction to graphs

    • @arslanmuhammad4190
      @arslanmuhammad4190 Před 8 měsíci

      @@nikoo28 Thanks Sir

    • @nikoo28
      @nikoo28  Před 5 měsíci

      The complete playlist on graphs is now available: czcams.com/play/PLFdAYMIVJQHNFJQt2eWA9Sx3R5eF32WPn.html

  • @user-de3mk1zk7m
    @user-de3mk1zk7m Před 2 měsíci

    Can you please provide solution for Human Readable Time

    • @nikoo28
      @nikoo28  Před 2 měsíci

      Can you provide me a link to the problem?

  • @user-ei3ng5kt4n
    @user-ei3ng5kt4n Před 4 měsíci +1

    class Solution {
    public boolean isValid(String s) {
    //valid parentheses would mean that all types of opening parentheses are correctly matched with their corresponding closing parentheses, and they are properly nested within each other.
    Stack stack = new Stack();
    for(char par : s.toCharArray()){
    if(par == '('){
    stack.push(')');
    }else if(par == '{'){
    stack.push('}');
    }else if(par == '['){
    stack.push(']');
    }else if(stack.pop() != par){ this code is also working , but i dont know why at last else if condition we want 2 condition to check. can any one explain
    return false;
    }
    }
    return stack.isEmpty();
    }
    }

    • @ramanarao4646
      @ramanarao4646 Před měsícem

      @user-ei3ng5kt4n check for this input String s =")";

  • @jjoelthomas
    @jjoelthomas Před 7 měsíci

    Hi Nikhil, Your solutions are amazing. But for this particular problem, I feel this can be done in a better way instead of complicating it with a stack data structure. Please find my solution which was advised to me in an interview at Apexon UK. I find the solution to be intuitive and very simple.
    private static boolean isBalancedParanthesis(String s){
    do
    {
    if(s.contains("()")) s = s.replace("()", "");
    if(s.contains("[]")) s = s.replace("[]","");
    if(s.contains("{}")) s = s.replace("{}", "");
    }while(s.contains("[\\W]+"));
    if(s.length()>0) return false;
    else return true;
    }

    • @nikoo28
      @nikoo28  Před 7 měsíci +1

      that works as well, but I was also trying to give an idea about how to solve mathematical expressions also. :)
      You are also relying on "contains" functionality.

    • @jjoelthomas
      @jjoelthomas Před 7 měsíci

      @nikoo28 I don't mean to discredit you. Because I learn a lot from you. And this solution I posted just in case someone's looking for an easy solution. Anyway your videos are all amazing and you are really doing a great work