Longest Valid Parentheses | LeetCode 32 | Coders Camp

Sdílet
Vložit
  • čas přidán 26. 07. 2024
  • Code: coderscamp.wixsite.com/codeit...
    Link to Problem: leetcode.com/problems/longest...
    This video solves the problem "Longest Valid Parentheses" in O(n) Time and space complexity
    #Leetcode #coderscamp #whiteboardexplanation #codinginterview #stack #validparentheses

Komentáře • 14

  • @samridhshubham8109
    @samridhshubham8109 Před 5 měsíci +2

    Excellent, clean cut explanation

  • @RahulKumar-oq5oo
    @RahulKumar-oq5oo Před 14 dny

    good clean, short explanation.

  • @hakoHiyo271
    @hakoHiyo271 Před 3 lety +5

    Your explanation of algorithm is nice and straight forward. There are so many other videos making this more complicated that it needs to be. Thanks!

  • @Its_________bansal4821
    @Its_________bansal4821 Před 11 měsíci

    really great💯💯💯

  • @malayawasthi
    @malayawasthi Před 2 lety +2

    Crisp and to the point. Great !!!

  • @manthansrivastav9640
    @manthansrivastav9640 Před 2 lety +1

    nice explanation really liked it

  • @riyankadey102
    @riyankadey102 Před rokem

    Easiest one❤️..thank you

  • @ogundimuhezekiah845
    @ogundimuhezekiah845 Před rokem

    How was this solution accepted? I don't understand the magic going on here😂

  • @MEGANE34
    @MEGANE34 Před rokem

    Thank you for good explaining but i want to ask why index is starting -1?

  • @indra16
    @indra16 Před 2 lety +1

    you cant iterate Stack like that , that is not allowed in stack
    instead of that you can pop elements
    class Solution {
    public int longestValidParentheses(String s) {

    if(s == null || s.length()==0){
    return 0;
    }
    int max = 0;

    Stack stack = new Stack();

    for(int i=0; i

  • @prakashsingh1107
    @prakashsingh1107 Před rokem

    thanks

  • @SANJAYSINGH-jt4hf
    @SANJAYSINGH-jt4hf Před rokem

    Solution wont' work as iteration over stack is wrong. Instead try this same thing just from right to left iteration
    class Solution {
    public int longestValidParentheses(String s) {
    if(s==null || s.length()==1) return 0;
    ArrayDeque stk = new ArrayDeque();
    for(int i=s.length()-1;i>=0;i--){
    if(!stk.isEmpty() && s.charAt(i)=='(' && s.charAt(stk.peek())==')'){
    stk.pop();
    }
    else{stk.push(i);}
    }
    int index=-1,res=0;
    for(int n:stk){
    res=Math.max(res,n-index-1);
    index=n;
    }
    res= Math.max(res,s.length()-index-1); //ifafter ) there is prefect brack eg:)()()
    return res;
    }
    }