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
													
											
												
												- 백준
- Kaggle
- PYTHON
- 더현대서울 맛집
- Real or Not? NLP with Disaster Tweets
- 코로나19
- 캐치카페
- 프로그래머스
- 편스토랑
- Baekjoon
- 편스토랑 우승상품
- Docker
- ubuntu
- programmers
- hackerrank
- 프로그래머스 파이썬
- 금융문자분석경진대회
- dacon
- AI 경진대회
- Git
- 자연어처리
- 맥북
- leetcode
- 우분투
- 데이콘
- 파이썬
- ChatGPT
- github
- gs25
- SW Expert Academy
													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: trueExample 2:
Input: "()[]{}"
Output: trueExample 3:
Input: "(]"
Output: falseExample 4:
Input: "([)]"
Output: falseExample 5:
Input: "{[]}"
Output: trueSolution
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


SOMJANG/CODINGTEST_PRACTICE
1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.
github.com
'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
								
							
						
					
                    
                
					
					
					
					
					
					
				 
						
					 
					 
													 
													 
													