class Solution {
public:
bool isValid(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
stack<char> st;
for(int i=0;i<s.length();i++){
if(s[i]=='(')
st.push(s[i]);
else if(s[i]==')'){
if(st.empty()||st.top()!='(')
return false;
st.pop();
}
else if(s[i]=='{')
st.push(s[i]);
else if(s[i]=='}'){
if(st.empty()||st.top()!='{')
return false;
st.pop();
}
else if(s[i]=='[')
st.push(s[i]);
else if(s[i]==']'){
if(st.empty()||st.top()!='[')
return false;
st.pop();
}
else
return false;
}
return st.empty();
}
};
public:
bool isValid(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
stack<char> st;
for(int i=0;i<s.length();i++){
if(s[i]=='(')
st.push(s[i]);
else if(s[i]==')'){
if(st.empty()||st.top()!='(')
return false;
st.pop();
}
else if(s[i]=='{')
st.push(s[i]);
else if(s[i]=='}'){
if(st.empty()||st.top()!='{')
return false;
st.pop();
}
else if(s[i]=='[')
st.push(s[i]);
else if(s[i]==']'){
if(st.empty()||st.top()!='[')
return false;
st.pop();
}
else
return false;
}
return st.empty();
}
};