private static final Map<Character, Character> matchingParentsMap = new HashMap<>();
private static final Set<Character> openingParentSet = new HashSet<>();
static {
matchingParentsMap.put(')', '(');
matchingParentsMap.put(']', '[');
matchingParentsMap.put('}', '{');
openingParentSet.addAll(matchingParentsMap.values());
}
public static boolean hasMatchingParents(String input) {
try {
Stack<Character> parentStack = new Stack();
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
// Add to the stack for an opening parent
if (openingParentSet.contains(ch)) {
parentStack.push(ch);
}
if (matchingParentsMap.contains(ch)) {
Character lastParent = parentStack.pop();
if (lastParent != matchingParentsMap.get(ch)) {
return false;
}
}
}
return parentStack.isEmpty();
} catch (Stack.StackOverflowException soe) {
System.err.println("Stack StackOver");
} catch (Stack.StackUnderflowException sue) {
System.err.println("Stack StackUnder");
}
}
public static class MinimumStack{
private Stack<Integar> stack = new Stack<>;
private Stack<Integar> minimumStack = new Stack<>;
public void push(int data) throws
Stack.StackOverflowException,Stack.StackUnderflowException {
int min = data;
if (!miniStack.isEmpty()) {
if (min > minimumStack.peek()) {
min = minimumStack.peek();
}
}
stack.push(data);
minimumStack.push(min);
}
public int pop() throw Stack.StackUnderflowException {
minimumStack.pop();
return stack.pop();
}
public int getMinimum() throw Stack.StackUnderflowException {
return minimumStack.peek();
}
}