class Solution {
public:
/**
*
* @param s string字符串
* @return bool布尔型
*/
bool isValid(string s) {
// write code here
stack<char> st;
int i = 0;
while(i < s.length()){
if(s[i] == '(' || s[i] == '[' || s[i] == '{'){
st.push(s[i]);
}
else{
if(s[i] == ')'){
if(st.empty() || st.top() != '('){
return false;
}
else{
st.pop();
}
}
else if(s[i] == ']'){
if(st.empty() || st.top() != '['){
return false;
}
else{
st.pop();
}
}
else{
if(st.empty() || st.top() != '{'){
return false;
}
else{
st.pop();
}
}
}
i++;
}
if(!st.empty()){
return false;
}
return true;
}
};