Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

 

 

 1 bool isValid(string s) {
 2       vector<char> sta;  
 3       if(s.size() ==0) return false;  
 4       sta.push_back(s[0]);  
 5       for(int i =1; i< s.size(); i++)  
 6       {  
 7         if(s[i] == '(' || s[i] == '[' || s[i] == '{')  
 8         {  
 9          sta.push_back(s[i]);  
10           continue;  
11         }  
12         char current = sta.back();  
13         if(s[i] == ')' && current != '(')  
14           return false;  
15         if(s[i] == ']' && current != '[')  
16           return false;  
17         if(s[i] == '}' && current != '{')  
18           return false;  
19         sta.pop_back();  
20       }  
21       if(sta.size() !=0) return false;  
22       return true;  
23 }