/**
* Given a string containing just the characters '(', ')', '{', '}', '[' and ']',
* determine if the input string is valid.
*
* The brackets must close in the correct order, "()" and "()[]{}" are all
* valid but "(]" and "([)]" are not.
*/
import java.util.Map;
import java.util.HashMap;
import java.util.Stack;
public class ValidParentheses20 {
public boolean isValid(String s) {
char[] chars = s.toCharArray();
Stack st = new Stack();
for (char c: chars) {
if (isBackP(c)) {
if (st.empty()) {
return false;
} else {
char top = (char) st.pop();
if (c != top) {
return false;
}
}
} else {
st.push(pair(c));
}
}
return st.empty();
}
private boolean isBackP(char c) {
return c == '}' || c == ']' || c == ')';
}
private char pair(char c) {
if (c == '{') {
return '}';
} else if (c == '[') {
return ']';
} else {
return ')';
}
}
/**
* https://discuss.leetcode.com/topic/27572/short-java-solution
*/
public boolean isValid2(String s) {
Stack