-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path20.cpp
More file actions
37 lines (37 loc) · 1.24 KB
/
Copy path20.cpp
File metadata and controls
37 lines (37 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public:
bool isValid(string s) {
stack<char> parentheses;
for (char c:s) {
if (!parentheses.empty()) {
char first = parentheses.top(), second = c;
if (first == '(') {
if (second == ')')
parentheses.pop();
else if (second == ']' || second == '}')
return false;
else
parentheses.push(second);
} else if (first == '[') {
if (second == ']')
parentheses.pop();
else if (second == ')' || second == '}')
return false;
else
parentheses.push(second);
} else if (first == '{') {
if (second == '}')
parentheses.pop();
else if (second == ')' || second == ']')
return false;
else
parentheses.push(second);
}
} else
parentheses.push(c);
}
if(!parentheses.empty())
return false;
return true;
}
};