-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path00020-valid_parentheses.cpp
More file actions
45 lines (33 loc) · 891 Bytes
/
00020-valid_parentheses.cpp
File metadata and controls
45 lines (33 loc) · 891 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
37
38
39
40
41
42
43
44
45
// 20: Valid Parentheses
// https://leetcode.com/problems/valid-parentheses/
#include <iostream>
#include <stack>
using namespace std;
class Solution {
public:
// SOLUTION
bool isValid (string s) {
stack<char> parentheses;
for (auto &c : s) {
switch(c) {
case '{': parentheses.push('}'); break;
case '[': parentheses.push(']'); break;
case '(': parentheses.push(')'); break;
default:
if (parentheses.size()==0 || c!=parentheses.top())
return false;
else parentheses.pop();
}
}
return parentheses.empty();
}
};
int main() {
Solution o;
// INPUT
string s = "()[]{}";
// OUTPUT
auto result = o.isValid(s);
cout<<(result ? "true" : "false")<<endl;
return 0;
}