Balanced Brackets

Balanced Brackets

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'isBalanced' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts STRING s as parameter.
     */

    public static String isBalanced(String s) {
    // Write your code here
Stack<Character> stack = new Stack<Character>();
for(int i = 0; i < s.length(); i++){
    if("{([".contains(s.charAt(i)+"")) stack.push(s.charAt(i));
    else if(s.charAt(i) == ']'){
        if(stack.isEmpty()) return "NO";
        if(stack.peek() != '[') return "NO";
        stack.pop();
                }
    else if(s.charAt(i) == '}'){
        if(stack.isEmpty()) return "NO";
        if(stack.peek() != '{') return "NO";
        stack.pop();
                }
    else if(s.charAt(i) == ')'){
        if(stack.isEmpty()) return "NO";
        if(stack.peek() != '(') return "NO";
        stack.pop();
                }
}
if(!stack.isEmpty()) return "NO";
return "YES";

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int t = Integer.parseInt(bufferedReader.readLine().trim());

        IntStream.range(0, t).forEach(tItr -> {
            try {
                String s = bufferedReader.readLine();

                String result = Result.isBalanced(s);

                bufferedWriter.write(result);
                bufferedWriter.newLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s