-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20_valid_parentheses.cpp
More file actions
35 lines (31 loc) · 863 Bytes
/
Copy path20_valid_parentheses.cpp
File metadata and controls
35 lines (31 loc) · 863 Bytes
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
class Solution {
public:
bool isValid(string s) {
stack p = stack<char>();
int len = s.size();
for (char c: s) {
if (c == '(' || c == '[' || c == '{'){
p.push(c);
} else {
if (p.empty())
return false;
char t = p.top();
cout << "comparing " << t << ' ' << c << endl;
if (!isMatching(t, c))
return false;
p.pop();
}
}
if (p.empty())
return true;
return false;
}
bool isMatching(char open, char close) {
switch (close) {
case ')': return open == '(';
case ']': return open == '[';
case '}': return open == '{';
default: return false;
}
}
};