LeetCode 20. Valid Parentheses, solved using Python

Опубликовано: 01 Июль 2021
на канале: buckmasterinstitute
355
1

LeetCode 20. Valid Parentheses | Python

Created and recorded by Aman Chhina. June 2021

Overview

1. Understand the problem.
2. The intuition of how to tackle the problem
3. Develop algorithm
4. Code the solution.

Script

Link to Problem: https://leetcode.com/problems/valid-p...

Introduction to the problem, and clarify what constitutes a valid parentheses sequence.

Clarify our intuition of how we would visually look at the sequence to determine whether it is valid or invalid.

Translate this to an appropriate algorithm we can use, which will be based on updating the last opening parentheses with the first closed parentheses.

Explain how a Stack and it's LIFO policy will be a best fit for our algorithm and go through rough details of what the algorithm will look like, as well as some edge cases such as the stack being empty when looking at a closing parentheses or the stack still having elements after iterating over the complete string.

Code up our algorithm, explaining the different cases and edge cases. Simplify our solution by using a dictionary to check whether the current character's valid open parenthesis corresponds to the element that's located in our stack.