Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Python/20_ValidParentheses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
#Difficulty: Easy
#Runtime: 82.23%
#Memory Usage: 64.83%

"""
Algorithm:
1. Check a bracket (b) in the string (s), if it contains in parentheses
dictionary then we add to stack it's opposite bracket.
2. If bracket not in parentheses we check does it mach with last bracket in
the stack, also we check if stack exists while iterating through the string.
If one of this conditions is not True - return False immediately.
3. After we checked whole string we need to check is stack empty or not and
return result.
4. Result will be true if stack is empty it means all parentheses are valid
and closed in the correct order.

Time Complexity = O(n)
Space Complexity = O(n)
"""

class Solution:
def isValid(self, s: str) -> bool:
parentheses = {'(':')', '{':'}', '[':']'}
stack = []
for b in s: # take bracket 'b' from string 's'
if b in parentheses: # if bracket in parentheses
stack.append(parentheses[b]) # append it's opposite to stack
elif not stack or stack.pop() != b: # if not stack or bracket not
return False # equal last bracket in stack
return not stack # if stack still exists -> False else True
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@
</div>
<br/>

## Stack
| # | Title | Solution | Time | Space | Difficulty | Tag | Note|
|-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----|
|020|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)|[Python](./Python/20_ValidParentheses.py)|_O(n)_|_O(n)_|Easy|Stack||

<br/>
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
</div>
<br/>


## Graph
| # | Title | Solution | Time | Space | Difficulty | Tag | Note|
|-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----|
Expand Down