class Solution {
public:
bool isValid(string s) {
stack<char> charstack;
size_t i=0;
while(i<s.length())
{
char c=s[i];
if(c!=')'&&c!='}'&&c!=']')
charstack.push(c);
else
{
if(charstack.size()==0)
return false;
char pre=charstack.top();
switch(c)
{
case ')':
if(pre=='(')
charstack.pop();
else
return false;
break;
case '}':
if(pre=='{')
charstack.pop();
else
return false;
break;
case ']':
if(pre=='[')
charstack.pop();
else
return false;
break;
}
}
i++;
}
if(charstack.size()==0)
return true;
else
return false;
}
};
public:
bool isValid(string s) {
stack<char> charstack;
size_t i=0;
while(i<s.length())
{
char c=s[i];
if(c!=')'&&c!='}'&&c!=']')
charstack.push(c);
else
{
if(charstack.size()==0)
return false;
char pre=charstack.top();
switch(c)
{
case ')':
if(pre=='(')
charstack.pop();
else
return false;
break;
case '}':
if(pre=='{')
charstack.pop();
else
return false;
break;
case ']':
if(pre=='[')
charstack.pop();
else
return false;
break;
}
}
i++;
}
if(charstack.size()==0)
return true;
else
return false;
}
};