-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e652d20
commit 1d4d9b9
Showing
2 changed files
with
12 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
# O(n) time || O(n) space | ||
def is_valid(self, s: str) -> bool: | ||
close_to_open, stack = {")": "(", "]": "[", "}": "{"}, [] | ||
for c in s: | ||
if c not in close_to_open: | ||
stack.append(c) | ||
elif stack: | ||
if stack.pop() != close_to_open[c]: | ||
brackets = {')': '(', ']': '[', '}': '{'} | ||
stack = [] | ||
for br in s: | ||
if br in brackets: | ||
if not stack or stack.pop() != brackets[br]: | ||
return False | ||
else: | ||
return False | ||
stack.append(br) | ||
|
||
return not stack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters