1 class Solution {
2 public:
3 bool isValid(string s) {
4 stack<char>q;
5 for (int i = 0; i < s.length(); i++)
6 {
7 char ch = s[i];
8 if (!q.empty())
9 {
10 if (ch == ')' &&q.top() == '(' ||
11 ch == ']' &&q.top() == '[' ||
12 ch == '}' &&q.top() == '{')
13 q.pop();
14 else
15 q.push(ch);
16 }
17 else
18 q.push(ch);
19 }
20 int ret = false;
21 if (q.empty())
22 ret = true;
23
24 return ret;
25 }
26 };
27