You are viewing a single comment's thread. Return to all comments →
Here is an alternative Java solution:
public static boolean isBalanced(String s) { String left = "{[("; String right= "}])"; if(s.length()%2 != 0)return false; Stack<Character> stack = new Stack<>(); for(int i = 0; i < s.length(); i++){ if(left.indexOf(s.charAt(i)) > -1){ stack.push(s.charAt(i)); } else{ if(stack.isEmpty())return false; char pop = stack.pop(); int indexR = right.indexOf(s.charAt(i)); int indexL = left.indexOf(pop); if(indexR != indexL)return false; } } return stack.isEmpty(); }
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 is an alternative Java solution: