app.codility.com/programmers/lessons/7-stacks_and_queues/brackets/
Brackets coding task - Learn to Code - Codility
Determine whether a given string of parentheses (multiple types) is properly nested.
app.codility.com
# 괄호 짝이 맞는지
def solution(S):
if not S:
return 1
st = []
for ch in S:
if ch in '{[(':
st.append(ch)
elif ch == ')':
if st and st[-1] == '(':
st.pop()
else:
return 0
elif ch == ']':
if st and st[-1] == '[':
st.pop()
else:
return 0
elif ch == '}':
if st and st[-1] == '{':
st.pop()
else:
return 0
if st:
return 0
return 1
print(solution("{[()()]}")) # 1
'Codility' 카테고리의 다른 글
[Lesson 9][Maximum slice problem] MaxDoubleSliceSum (0) | 2021.05.07 |
---|---|
[Lesson 8][Leader] Dominator (0) | 2021.05.07 |
[Lesson 6][Sorting] Distinct (0) | 2021.05.07 |
[Lesson 5][Prefix Sums] CountDiv (0) | 2021.05.07 |
[Lesson 4][Counting Elements] FrogRiverOne (0) | 2021.05.07 |