Valid Parentheses
class Solution {
public:
bool isValid(string s) {
stack<char>stk;
int str_len = s.length();
for(int i=0;i<str_len;i++){
if(s[i] == '{' ||s[i] == '['||s[i] == '(' ){
stk.push(s[i]);
}
else{
//notice the order of twe paras in the two sides of '&&'
if(!stk.empty() && is_valid_pair(stk.top(),s[i])){
stk.pop();
}
else{
return false;
}
}
}
if(stk.empty())
return true;
return false;
}
bool is_valid_pair(char a,char b){
return (a =='[' && b==']')||(a =='{' && b=='}')||(a =='('&& b==')');
}
};