Skip to content

Commit a8a84fb

Browse files
Merge pull request #2154 from CodingTestStudy2/변지협
[변지협] 2026/10/09
2 parents 03a0843 + d6a1938 commit a8a84fb

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
1. 아이디어 :
3+
스택을 이용한다. 여는 괄호나 스택 top과 짝이 맞지 않는 문자는 push하고,
4+
스택 top과 짝이 맞는 닫는 괄호가 나오면 pop한다.
5+
모든 문자를 처리한 뒤 스택이 비어 있으면 유효한 괄호 문자열, 아니면 유효하지 않다.
6+
7+
2. 시간복잡도 :
8+
o(n)
9+
10+
3. 자료구조/알고리즘 :
11+
스택
12+
13+
'''
14+
15+
class Solution:
16+
def isValid(self, s: str) -> bool:
17+
stack = []
18+
for e in s:
19+
if not stack:
20+
stack.append(e)
21+
continue
22+
23+
if stack[len(stack)-1] == '(' and e == ')':
24+
stack.pop()
25+
continue
26+
elif stack[len(stack)-1] == '[' and e == ']':
27+
stack.pop()
28+
continue
29+
elif stack[len(stack)-1] == '{' and e == '}':
30+
stack.pop()
31+
continue
32+
33+
stack.append(e)
34+
35+
return True if not stack else False

0 commit comments

Comments
 (0)