-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path020.cpp
34 lines (30 loc) · 922 Bytes
/
020.cpp
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
// https://www.youtube.com/watch?v=QZOLb0xHB_Q
class Solution {
public:
bool isValid(std::string Input) {
std::stack<char> Stack;
for (auto C : Input) {
if (isOpeningParentheses_(C)) {
Stack.push(C);
continue;
}
if (!Stack.empty() && isClosingParentheses_(C) && isMatch_(Stack.top(), C)) {
Stack.pop();
} else {
return false;
}
}
return Stack.empty();
}
bool isOpeningParentheses_(char C) {
return C == '(' || C == '[' || C == '{';
}
bool isClosingParentheses_(char C) {
return C == ')' || C == ']' || C == '}';
}
bool isMatch_(char Opening, char Closing) {
return (Opening == '(' && Closing == ')')
|| (Opening == '{' && Closing == '}')
|| (Opening == '[' && Closing == ']');
}
};