Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- SW Expert Academy
- ChatGPT
- 데이콘
- leetcode
- 편스토랑
- 백준
- 파이썬
- gs25
- Kaggle
- 맥북
- hackerrank
- Docker
- programmers
- Real or Not? NLP with Disaster Tweets
- 자연어처리
- dacon
- AI 경진대회
- Baekjoon
- 금융문자분석경진대회
- Git
- 우분투
- 프로그래머스
- 더현대서울 맛집
- github
- 프로그래머스 파이썬
- 캐치카페
- 편스토랑 우승상품
- PYTHON
- ubuntu
- 코로나19
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 20. Valid Parentheses (Python) 본문
728x90
반응형
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
Input: "()"
Output: true
Example 2:
Input: "()[]{}"
Output: true
Example 3:
Input: "(]"
Output: false
Example 4:
Input: "([)]"
Output: false
Example 5:
Input: "{[]}"
Output: true
Solution
class Solution:
def isValid(self, s: str) -> bool:
s_list = list(s)
answer = True
stack = []
for s in s_list:
if s == '[' or s == '(' or s == '{':
stack.append(s)
elif len(stack) != 0:
if (s == ']' and stack[-1] == '[') or (s == '}' and stack[-1] == '{') or (s == ')' and stack[-1] == '('):
stack.pop()
else:
answer = False
break
else:
answer = False
break
if len(stack) != 0:
answer = False
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 633. Sum of Square Numbers (Python) (0) | 2020.06.19 |
---|---|
[leetCode] 283. Move Zeroes (Python) (0) | 2020.06.19 |
[leetCode] 125. Valid Palindrome (Python) (0) | 2020.06.16 |
[leetCode] 387. First Unique Character in a String (Python) (0) | 2020.06.15 |
[leetCode] 88. Merge Sorted Array (Python) (0) | 2020.06.14 |
Comments