Skip to content

Commit 065a79d

Browse files
committed
Solved "Valid parentheses" problem
1 parent 1bc8c62 commit 065a79d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
1+
"""
2+
Constraints:
3+
1. 1 <= s.length <= 10^4
4+
2. s consists of parentheses only '()[]{}'
15
6+
Time Complexity: O(n)
7+
- λ¬Έμžμ—΄μ˜ 각 문자λ₯Ό ν•œ λ²ˆμ”©λ§Œ μˆœνšŒν•˜λ―€λ‘œ O(n)
8+
- 각 λ¬Έμžμ— λŒ€ν•œ μ—°μ‚°(push, pop)은 O(1)
9+
10+
Space Complexity: O(n)
11+
- μ΅œμ•…μ˜ 경우 λͺ¨λ“  λ¬Έμžκ°€ μ—¬λŠ” κ΄„ν˜ΈμΌ λ•Œ μŠ€νƒμ— λͺ¨λ‘ μ €μž₯
12+
- λ”°λΌμ„œ μž…λ ₯ 크기에 λΉ„λ‘€ν•˜λŠ” O(n) 곡간 ν•„μš”
13+
"""
14+
class Solution:
15+
def isValid(self, s: str) -> bool:
16+
stack = []
17+
pairs = {')': '(', '}': '{', ']': '['}
18+
19+
for char in s:
20+
if char in '({[':
21+
stack.append(char)
22+
else:
23+
if not stack or stack[-1] != pairs[char]:
24+
return False
25+
stack.pop()
26+
27+
return len(stack) == 0

0 commit comments

Comments
Β (0)