|
| 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 | +``` |
0 commit comments