-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParanthesisMatching.cpp
45 lines (39 loc) · 1.19 KB
/
ParanthesisMatching.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
35
36
37
38
39
40
41
42
43
44
45
// you can use includes, for example:
// #include <algorithm>
#include <stack>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
std::stack<char> strStack;
char c;
for(size_t i = 0; i < S.length(); i++) {
if(S[i] == '{' || S[i] == '(' || S[i] == '[') {
strStack.push(S.at(i));
continue;
}
if(strStack.empty())
return 0;
switch(S[i]) {
case ')':
c = strStack.top();
strStack.pop();
if(c == '{' || c == '[')
return 0;
break;
case '}':
c = strStack.top();
strStack.pop();
if(c == ')' || c == '[')
return 0;
break;
case ']':
c = strStack.top();
strStack.pop();
if(c == '{' || c == '(')
return 0;
break;
}
}
return (strStack.empty());
}