Leetcode 20.
Valid Parentheses
class Solution {
public:
bool bracketMatch(char opening,char closing){
return (opening=='(' && closing==')') ||
(opening=='{' && closing=='}') || (opening=='[' &&
closing==']');
}
bool isValid(string s) {
stack<char> st;
for(auto x:s){
if(x=='(' || x=='{' || x=='[')
st.push(x);
else{
if(st.empty())
return false;
if(bracketMatch(st.top(),x)){
st.pop();
}
else{
return false;
}
}
}
return st.empty();
}
};