Created
October 14, 2016 06:13
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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