-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLv2_괄호회전하기.cpp
58 lines (50 loc) · 1.19 KB
/
Lv2_괄호회전하기.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
46
47
48
49
50
51
52
53
54
55
56
57
58
int main() {
cout << solution("[](){}");
cout << solution("}]()[{");
cout << solution("[)(]");
return 0;
}#include <string>
#include <vector>
#include <stack>
#include <iostream>
using namespace std;
bool check(string s) {
stack<char> st;
bool flag = false;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '(')
st.push('(');
else if (s[i] == '{')
st.push('{');
else if (s[i] == '[')
st.push('[');
else
{
if (!st.empty()) {
flag = true;
if (st.top() == '(' && s[i] == ')')
st.pop();
else if (st.top() == '{' && s[i] == '}')
st.pop();
else if (st.top() == '[' && s[i] == ']')
st.pop();
else
st.push(s[i]);
}
}
}
if (st.empty() && flag)
return true;
else
return false;
}
int solution(string s) {
int answer = 0;
for (int i = 0; i < s.length(); ++i) {
s += s[0];
s = s.substr(1, s.length() - 1);
if (check(s))
++answer;
}
return answer;
}