Exercises
We will resolve a few array exercises from LeetCode using the concepts we learned in this chapter.
Valid Parentheses
The first exercise we will resolve is the 20. Valid Parentheses problem available at https://leetcode.com/problems/valid-parentheses/.
When resolving the problem using JavaScript or TypeScript, we will need to add our logic inside the function function isValid(s: string): boolean {}
, which receives a string it is expecting a boolean to be returned.
The following are the sample input and expected output provided by the problem:
- Input "()", output true.
- Input "()", output true.
- Input "(]", output false.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
- Every close bracket has a corresponding open bracket of the same type.
The problem also provides three hints, which contain the logic we need to implement to resolve this problem:
- Use a stack...