Identify the Approach Flashcards
Validate Parentheses
You are given a string s consisting of the following characters: ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’.
The input string s is valid if and only if:
Every open bracket is closed by the same type of close bracket.
Open brackets are closed in the correct order.
Every close bracket has a corresponding open bracket of the same type.
Return true if s is a valid string, and false otherwise.
- Append all open bracket to a stack.
- When a closing bracket is there check for matching bracket.
- If match pop open bracket from stack.
- If the stack is empty it should return true.
Generate Parentheses
You are given an integer n. Return all well-formed parentheses strings that you can generate with n pairs of parentheses.
Example 1:
Input: n = 1
Output: [”()”]
Example 2:
Input: n = 3
Output: [”((()))”,”(()())”,”(())()”,”()(())”,”()()()”]
You may return the answer in any order.
- Only add a open < n
- only add a close < open
- Valid If open == close == n
- Use Recursion to backtrack .