You are viewing a single comment's thread. Return to all comments →
private static boolean isMatch(String line) { if (line.length()%2 != 0) { return false; } Stack<Integer> intStack = new Stack<>(); for (char c : line.toCharArray()) { switch (c) { case '[': intStack.push(1); break; case '{': intStack.push(2); break; case '(': intStack.push(3); break; case ')': intStack.push(4); break; case '}': intStack.push(5); break; case ']': intStack.push(6); break; default: // } } Stack<Integer> leftoverStack = new Stack<>(); while (intStack.size() > 0) { if (leftoverStack.isEmpty()) { leftoverStack.push(intStack.pop()); } else { if (intStack.peek() + leftoverStack.peek() == 7 && intStack.peek() < leftoverStack.peek()) { intStack.pop(); leftoverStack.pop(); } else { leftoverStack.push(intStack.pop()); } } } return leftoverStack.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 →