Java Program to check Valid Parenthesis or Balanced Parenthesis| Valid Parentheses Leetcode solution

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ม.ค. 2025

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

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

    Improvements in your version of code, with 100% test cases passed on GFG.
    class GFG {
    // Function to check if brackets are balanced or not.
    static boolean ispar(String s) {
    // add your code here
    if (s.isEmpty()) {
    return false;
    }
    Stack stack = new Stack();
    for (int i = 0; i < s.length(); i++) {
    Character c = s.charAt(i);
    if (c == '(' || c == '{' || c == '[') {
    stack.push(c);
    }
    if (stack.isEmpty())
    return false;
    Character a;
    switch (c) {
    case ')':
    a = stack.pop();
    if (a == '{' || a == '[')
    return false;
    break;
    case '}':
    a = stack.pop();
    if (a == '(' || a == '[')
    return false;
    break;
    case ']':
    a = stack.pop();
    if (a == '{' || a == '(')
    return false;
    break;
    }
    }
    if (stack.isEmpty()) {
    return true;
    }
    return false;
    }
    }

  • @harsimranjitsingh4589
    @harsimranjitsingh4589 2 ปีที่แล้ว +1

    Before the for loop, we can add an if condition where if the length of the input is odd, we can return false.

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

      Yeah, agree, if the length is off we don’t need to proceed we can return false because we are sure that its not balanced.
      Thank you so much

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

    Please do video on how to find complexity of a program in simple way

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

      Be a pro, find it like a pro 😁
      I will create one for sure atleast for time complexity

  • @harshitsachdeva4836
    @harshitsachdeva4836 2 ปีที่แล้ว +1

    Bring more questions like this

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

    which screen recorder and mic u r using ?

  • @vineethchinna3951
    @vineethchinna3951 2 ปีที่แล้ว +1

    please make a video on what all tools used by java web application developer in his day to day activities.

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

      Sure, coming in couple of days

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

      Its going to be out today @9 pm

    • @vineethchinna3951
      @vineethchinna3951 2 ปีที่แล้ว +1

      @@JavaTechies thanks waiting for it.

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

    I write my code in same model but I use if else conditions to check.
    But I'm encountering with an error that is Bad Operand types for binary operator '== '.
    Can you help me!

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

      Can you share complete if condition

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

    I think, there is a mistake in if conditions(2nd onwards) of switch case. isn't it should match the opening brackets.

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

      If so please send updated code in comments

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

      I will pin the code in first comment