title: Balanced Parenthesis:
body: <p>I am trying to solve a problem of balanced parenthesis but getting error. I don't know why I am getting wrong answer as my logic is fine. I am trying to solve it for a long time but can't get any way. This is my code in java:   </p>
  
  <pre><code>public class Solution {
  
      public static void main(String[] args) {
  
          Scanner sc=new Scanner(System.in);
          int n=sc.nextInt();
  
          for(int i=0; i&lt;n;i++){
               Stack&lt;Character&gt; stack=new Stack&lt;Character&gt;();
          char x,y;
             for(int j=0; j&lt;stack.size();j++){ 
             x=sc.next().charAt(j);
             y=stack.peek();
             if(x=='{' || x=='[' || x=='('){
                  stack.push(x);
              }
  
              if(x=='}'){
                  if(y=='{'){
                      stack.pop();
                  //    System.out.println("Yes");
                  }
                 // else{
                   //   System.out.println("No");
                 //}
  
              }else if(x==']'){
                  if(y=='['){
                      stack.pop();
  
                  }
              }else if(x==')'){   
                  if(y=='('){
                      stack.pop();
                  //    System.out.println("Yes");
                  }
              }
            }
           if(stack.empty()==true){
           System.out.println("YES");
  
      }else if(stack.empty()!=true){
           System.out.println("NO");   
          }
      }
  
  
      }
  }
  </code></pre>