You are viewing a single comment's thread. Return to all comments →
My Java solution:
public static String isBalanced(String s) { Stack<Character> stack = new Stack<>(); HashMap<Character, Character> map = new HashMap<>(); map.put(')', '('); map.put(']', '['); map.put('}', '{'); // \ ({}) \ \ )({}) \ for (char c : s.toCharArray()) { if (map.containsKey(c)) { if (!stack.isEmpty() && stack.peek() == map.get(c)) { stack.pop(); } else { return "NO"; } } else { stack.push(c); } } 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 →
My Java solution: