Identify the Approach Flashcards

1
Q

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.

A
  1. Append all open bracket to a stack.
  2. When a closing bracket is there check for matching bracket.
  3. If match pop open bracket from stack.
  4. If the stack is empty it should return true.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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.

A
  1. Only add a open < n
  2. only add a close < open
  3. Valid If open == close == n
  4. Use Recursion to backtrack .
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly