-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLv.2 괄호 회전하기.py
41 lines (40 loc) · 1.36 KB
/
Lv.2 괄호 회전하기.py
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
#https://school.programmers.co.kr/learn/courses/30/lessons/76502
def solution(s):
answer = 0
for i in range(len(s)):
stack = []
right = False
count = 0
for j in range(i, len(s)):
count += 1
if s[j] == "[" or s[j] == "{" or s[j] == "(":
stack.append(s[j])
else:
if stack:
w = stack.pop()
if (w == "[" and s[j] == "]") or (w == "{" and s[j] == "}") or (w == "(" and s[j] == ")"):
right = True
else:
right = False
break
else:
right = False
break
for j in range(i):
count += 1
if s[j] == "[" or s[j] == "{" or s[j] == "(":
stack.append(s[j])
else:
if stack:
w = stack.pop()
if (w == "[" and s[j] == "]") or (w == "{" and s[j] == "}") or (w == "(" and s[j] == ")"):
right = True
else:
right = False
break
else:
right = False
break
if right and not stack and count == len(s):
answer += 1
return answer