Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ad533f5

Browse files
Added solution for the Valid Parentheses problem
Signed-off-by: Krishna Kishore Shetty <kkshetty.work@pm.me>
1 parent fc88482 commit ad533f5

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
File renamed without changes.

‎valid_parentheses/Notes.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Original: https://leetcode.com/problems/valid-parentheses/solutions/5033286/stack-based-python3-solution-beats-94-o-n/
2+
3+
# Intuition
4+
We use a stack as the centerpiece of this solution. If we see an opening brace, push to stack. If we see a Closing brace, we pop the top of stack to see if there is matching opening brace. Rinse and repeat and we should have an empty stack at the end, in the ideal case.
5+
6+
# Approach
7+
We use a Python list as a Stack.
8+
9+
We traverse the input character by character.
10+
For each character, we check what kind of brace it is. So there are two cases to handle.
11+
1. Case 1 - Opening brace - `(,{,[` - In this case, we simply push to the stack and continue to the next iteration.
12+
2. Case 2 - Closing brace - `),},[` - This is a bit more complex. If the stack is empty, then we immediately return `False`, as it is a parentheses mismatch. If the stack is not empty, we pop the top of the stack, and check if the top is the opening brace of the same type. If it is, the braces match, and we continue to the next iteration.
13+
14+
If we have traversed the entire input, and the stack is empty, then it meanns the input is valid, and we return `True`. If the stack is not empty, then there was a parentheses mismatch, and input is invalid, and we return `False`.
15+
16+
# Complexity
17+
- Time complexity: $$O(n)$$
18+
We make one traversal of the entire input, which is O(n). During which we check the type of braces, which are constant time operations.
19+
20+
- Space complexity: $$O(n)$$
21+
Size of stack grows with the size of input. To be more precise, size of stack grows with depth of nesting of the parentheses. Worst case is O(n).
22+
23+
24+
# Code
25+
```python
26+
class Solution:
27+
def isValid(self, s: str) -> bool:
28+
stack = []
29+
for brace in s:
30+
match brace:
31+
# Opening brace, push to the stack
32+
case '(' | '{' | '[':
33+
stack.append(brace)
34+
continue
35+
# Closing brace
36+
case ')' | '}' | ']':
37+
# Pop stack and check to see if top of stack is matching opening brace
38+
# If not matching, return false immediately
39+
# Edge case: Handle encounter of closing brace before opening brace
40+
if not stack:
41+
return False
42+
else:
43+
stack_top = stack.pop()
44+
45+
if self.is_matching_brace(stack_top, brace):
46+
continue
47+
else:
48+
return False
49+
50+
# Check if stack empty
51+
if not stack:
52+
return True
53+
else:
54+
return False
55+
56+
def is_matching_brace(self, opening: str, closing: str) -> bool:
57+
if (opening=='(' and closing==')') or (opening=='{' and closing=='}') or (opening=='[' and closing==']'):
58+
return True
59+
return False
60+
```

‎valid_parentheses/Solution.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
stack = []
4+
for brace in s:
5+
match brace:
6+
# Opening brace, push to the stack
7+
case '(' | '{' | '[':
8+
stack.append(brace)
9+
continue
10+
# Closing brace
11+
case ')' | '}' | ']':
12+
# Pop stack and check to see if top of stack is matching opening brace
13+
# If not matching, return false immediately
14+
# Also handle encounter of closing brace before opening brace
15+
if not stack:
16+
return False
17+
else:
18+
stack_top = stack.pop()
19+
20+
if self.is_matching_brace(stack_top, brace):
21+
continue
22+
else:
23+
return False
24+
25+
# Check if stack empty
26+
if not stack:
27+
return True
28+
else:
29+
return False
30+
31+
def is_matching_brace(self, opening: str, closing: str) -> bool:
32+
if (opening=='(' and closing==')') or (opening=='{' and closing=='}') or (opening=='[' and closing==']'):
33+
return True
34+
return False

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /