You are viewing a single comment's thread. Return to all comments →
Here's the same solution, but neatened up
public static boolean isBalanced(String expression) { Stack<Character> brackets = new Stack<Character>(); for(int i=0; i<expression.length(); i++){ char next = expression.charAt(i); if(next == '{') brackets.add('}'); else if(next == '[') brackets.add(']'); else if(next == '(') brackets.add(')'); else if(brackets.empty() || brackets.pop() != next) { return false; } } return brackets.empty(); }
Seems like cookies are disabled on this browser, please enable them to open this website
Stacks: Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
Here's the same solution, but neatened up