You are viewing a single comment's thread. Return to all comments →
Simple java solution in O(n) 100%:
public static String isBalanced(String s) { Stack<Character> stack = new Stack<>(); Map<Character, Character> dict = Map.of('{','}', '[',']', '(',')'); for (char c : s.toCharArray()) { if (dict.containsKey(c)) stack.push(dict.get(c)); else if (stack.isEmpty() || c != stack.pop()) 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 →
Simple java solution in O(n) 100%: