-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidParentheses
More file actions
36 lines (34 loc) · 974 Bytes
/
Copy pathvalidParentheses
File metadata and controls
36 lines (34 loc) · 974 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
36
class Solution {
public:
bool isValid(string s) {
int round = 0;
int curly = 0;
int square = 0;
for (char ch : s) {
if (ch == '(') {
round++;
} else if (ch == ')') {
round--;
if (round < 0) return false;
}
if (ch == '{') {
curly++;
} else if (ch == '}') {
curly--;
if (curly < 0) return false;
}
if (ch == '[') {
square++;
} else if (ch == ']') {
square--;
if (square < 0) return false;
}
if ((round > 0 && (curly > 0 || square > 0)) ||
(curly > 0 && (round > 0 || square > 0)) ||
(square > 0 && (round > 0 || curly > 0))) {
return false;
}
}
return round == 0 && curly == 0 && square == 0;
}
};