Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions programmers/올바른괄호.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 시간복잡도 O(n) n:문자열의 크기

def solution(s):
answer = True
count = 0
right = 0 #'(' 개수
left = 0 #')' 개수
for idx in range(len(s)):
if s[idx] == '(':
count += 1 # '('가 나오면 더해줌
right += 1
else:
left += 1
if count > 0: # '('가 한번이상 나왔을 때
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 count가 한번이라도 -1로 가면 바로 False 리턴하도록 짰습니다.
어차피 )가 ()쌍이나 ( 앞에 하나라도 있으면 False기 때문에...
그래도 답엔 이상없으니 괜찮은듯

count -= 1 # ')'가 나오면 빼줌

if (count != 0) | (left != right): # count가 0이 아니거나 '('와 ')' 개수가 다르면 false
answer = False
return answer