Skip to content

Commit 1e5b14b

Browse files
authored
Resolved 'Valid Parentheses' LeetCode problem with an optimized solution
1 parent c7a813f commit 1e5b14b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Python/valid_parentheses.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Valid Parentheses
2+
3+
class Solution(object):
4+
def isValid(self, s):
5+
6+
stack = []
7+
mapping = {')': '(', '}': '{', ']': '['}
8+
9+
for char in s:
10+
if char in mapping.values():
11+
stack.append(char)
12+
elif char in mapping.keys():
13+
if not stack or mapping[char] != stack.pop():
14+
return False
15+
else:
16+
continue
17+
18+
return not stack
19+
20+
solution = Solution()
21+
print(solution.isValid("()"))

0 commit comments

Comments
 (0)