Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool isValid(string s) {
- string s1 = "";
- for(int i=0;i<s.length();i++)
- {
- switch(s1.back())
- {
- case '(': if ( (s[i]=='}') || (s[i]==']') )
- return false;
- if ( s[i]==')' )
- {
- s1.pop_back();
- break;
- }
- s1.push_back(s[i]);
- break;
- case '{': if ( (s[i]==')') || (s[i]==']') )
- return false;
- if ( s[i]=='}' )
- {
- s1.pop_back();
- break;
- }
- s1.push_back(s[i]);
- break;
- case '[': if ( (s[i]==')') || (s[i]=='}') )
- return false;
- if ( s[i]==']' )
- {
- s1.pop_back();
- break;
- }
- s1.push_back(s[i]);
- break;
- default: s1.push_back(s[i]);
- }
- }
- if (s1=="")
- return true;
- else return false;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement