You are viewing a single comment's thread. Return to all comments →
I have improved and cleaned up your code. Here the effect:
private static String isBalancedBrackets(String value) { Stack<Character> stack = new Stack<>(); char upperElement = 0; for (int i = 0; i < value.length(); i++) { if (!stack.isEmpty()) { upperElement = stack.peek(); } stack.push(value.charAt(i)); if (!stack.isEmpty() && stack.size() > 1) { if ((upperElement == '[' && stack.peek() == ']') || (upperElement == '{' && stack.peek() == '}') || (upperElement == '(' && stack.peek() == ')')) { stack.pop(); stack.pop(); } } } return stack.isEmpty() ? "YES" : "NO";
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
I have improved and cleaned up your code. Here the effect: