Skip to content

Commit 13a1d69

Browse files
committed
added 20. Valid Parentheses
1 parent c15a1d7 commit 13a1d69

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

leetcode.com/valid-parentheses.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# https://leetcode.com/problems/valid-parentheses/
2+
3+
# Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
4+
#
5+
# An input string is valid if:
6+
#
7+
# Open brackets must be closed by the same type of brackets.
8+
# Open brackets must be closed in the correct order.
9+
# Every close bracket has a corresponding open bracket of the same type.
10+
11+
12+
# Input: s = "()[]{}"
13+
# Output: true
14+
#
15+
# Input: s = "(]"
16+
# Output: false
17+
18+
class Solution:
19+
def isValid(self, s: str) -> bool:
20+
while '()' in s or '{}' in s or '[]' in s:
21+
s = s.replace('()', '')
22+
s = s.replace('{}', '')
23+
s = s.replace('[]', '')
24+
# or one line
25+
# s = s.replace('()', '').replace('{}', '').replace('[]', '')
26+
27+
return len(s) == 0

0 commit comments

Comments
 (0)