Skip to content

Instantly share code, notes, and snippets.

@feliperazeek
Created October 14, 2016 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feliperazeek/2d886d000e33202c88b4462d0ed3fd92 to your computer and use it in GitHub Desktop.
Save feliperazeek/2d886d000e33202c88b4462d0ed3fd92 to your computer and use it in GitHub Desktop.
HackerRank - Cracking the Code Interview - Stacks: Balanced Brackets (https://www.hackerrank.com/challenges/ctci-balanced-brackets)
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
private static List<Character> openChars = new ArrayList<Character>(Arrays.asList('{', '(', '['));
private static List<Character> closeChars = new ArrayList<Character>(Arrays.asList('}', ')', ']'));
public static boolean isBalanced(String expression) {
if (expression == null) return true;
Stack<Character> stack = new Stack<Character>();
for (char c : expression.toCharArray()) {
if (openChars.contains(c)) stack.push(c);
if (closeChars.contains(c)) {
if (stack.empty()) return false;
char openChar = stack.pop();
if (!matches(openChar, c)) return false;
}
}
return stack.empty();
}
private static boolean matches(char c1, char c2) {
if (c1 == '(' && c2 == ')')
return true;
else if (c1 == '{' && c2 == '}')
return true;
else if (c1 == '[' && c2 == ']')
return true;
else
return false;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++) {
String expression = in.next();
boolean answer = isBalanced(expression);
if(answer)
System.out.println("YES");
else System.out.println("NO");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment