You are viewing a single comment's thread. Return to all comments →
Java solution using Deque
public static String isBalanced(String s) { Deque<Character> stack = new ArrayDeque<>(); for(int i = 0; i<s.length(); i++){ if(s.charAt(i)=='(' || s.charAt(i)=='['|| s.charAt(i)=='{'){ stack.push(s.charAt(i)); }else{ if(stack.isEmpty())return "NO"; if((s.charAt(i)==')' && stack.pop() =='(') || (s.charAt(i)==']' && stack.pop() =='[') || s.charAt(i)=='}' && stack.pop() =='{') continue; else return "NO"; } } return stack.isEmpty()? "YES":"NO"; }
Seems like cookies are disabled on this browser, please enable them to open this website
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
Java solution using Deque