programmers 20260428
This commit is contained in:
parent
f7850f7e17
commit
346727ddac
17
code_study/programmers/python/올바른 괄호/problem.txt
Normal file
17
code_study/programmers/python/올바른 괄호/problem.txt
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
괄호가 바르게 짝지어졌다는 것은 '(' 문자로 열렸으면 반드시 짝지어서 ')' 문자로 닫혀야 한다는 뜻입니다.
|
||||||
|
예를 들어
|
||||||
|
"()()" 또는 "(())()" 는 올바른 괄호입니다.
|
||||||
|
")()(" 또는 "(()(" 는 올바르지 않은 괄호입니다.
|
||||||
|
'(' 또는 ')' 로만 이루어진 문자열 s가 주어졌을 때, 문자열 s가 올바른 괄호이면 true를 return 하고,
|
||||||
|
올바르지 않은 괄호이면 false를 return 하는 solution 함수를 완성해 주세요.
|
||||||
|
|
||||||
|
제한사항
|
||||||
|
문자열 s의 길이 : 100,000 이하의 자연수
|
||||||
|
문자열 s는 '(' 또는 ')' 로만 이루어져 있습니다.
|
||||||
|
|
||||||
|
입출력 예
|
||||||
|
|
||||||
|
"()()" -> true
|
||||||
|
"(())()" -> true
|
||||||
|
")()(" -> false
|
||||||
|
"(()(" -> false
|
||||||
21
code_study/programmers/python/올바른 괄호/solution.py
Normal file
21
code_study/programmers/python/올바른 괄호/solution.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
s = "(()("
|
||||||
|
|
||||||
|
def solution(s) :
|
||||||
|
from collections import deque
|
||||||
|
stack = deque()
|
||||||
|
|
||||||
|
for c in s :
|
||||||
|
if c == "(" :
|
||||||
|
stack.append(c)
|
||||||
|
elif c == ")" :
|
||||||
|
if not stack :
|
||||||
|
return False
|
||||||
|
else :
|
||||||
|
stack.pop()
|
||||||
|
|
||||||
|
if stack :
|
||||||
|
return False
|
||||||
|
else :
|
||||||
|
return True
|
||||||
|
|
||||||
|
print(solution(s))
|
||||||
Loading…
x
Reference in New Issue
Block a user