Here is the problem statement. Print YES if the brackets in a given string are balanced, else print NO. Here is my O(N) submission to the challenge.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    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();
            System.out.println(new Solution().isBalanced(s) ? "YES" : "NO");
        }

    }

    public static boolean isBalanced(String expression){
        Stack<Character> s = new Stack<Character>();
        for(char c : expression.toCharArray()){
            if(c == '{' || c == '[' || c=='(') s.push(c);
            switch(c){
                case ']' : if(!s.isEmpty() && s.pop() == '[') continue; else return false;
                case '}' : if(!s.isEmpty() && s.pop() == '{') continue; else return false;
                case ')' : if(!s.isEmpty() && s.pop() == '(') continue; else return false;
            }
        }
        return s.isEmpty();
    }
}