Here is my code..
The program always return YES value. I don't know why?
// I have pasted down the question link below.
import java.io.*;import java.util.*;import java.text.*;import java.math.*;import java.util.regex.*;public class Solution {static String isBalanced(String s) {// Complete this functionStack<String> stack=new Stack<>();int q=s.length()-1;int i=0;while(i<=q){String p=""+s.charAt(i);if(p=="{"){stack.push(p);}if(p=="("){stack.push(p);}if(p=="["){stack.push(p);}if((p=="}" && (stack.peek()=="{")) || (p==")" && (stack.peek()=="(")) || (p=="]" && (stack.peek()=="["))){stack.pop();}i++;}if(stack.empty())return "YES";elsereturn "NO";}public static void main(String[] args) {Scanner in = new Scanner(System.in);int t = in.nextInt();for(int a0 = 0; a0 < t; a0++){String s = in.next();String result = isBalanced(s);System.out.println(result);}in.close();}}