You are viewing a single comment's thread. Return to all comments →
java 8
public static void main(String []argh) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String input=sc.next(); System.out.println(isBalanced(input)); } } public static boolean isBalanced(String s) { Map<Character, Character> braketsMap = new HashMap<>(); braketsMap.put('{', '}'); braketsMap.put('(', ')'); braketsMap.put('[', ']'); Stack<Character> stack = new Stack<>(); for (char ch: s.toCharArray()) { if (braketsMap.containsKey(ch)) { stack.add(ch); } else { if (stack.isEmpty()) { return false; } char last = stack.pop(); if (ch != braketsMap.get(last)) { return false; } } } return stack.isEmpty(); }
Seems like cookies are disabled on this browser, please enable them to open this website
Java Stack
You are viewing a single comment's thread. Return to all comments →
java 8