Parentheses Problems

Key Points

There are mainly 2 type problems relevant to parentheses

  1. Check if the parentheses in string are valid. Usually can be resolved with stack
  2. Generate valid parentheses in string. Usually can be resolve with backtrack

Thinking Patterns

  1. How to check if a string is valid parentheses?

     a. The number of '(' and ')' should be same, and,
     b. They must be in pair. To be more specific, there should be NO leading ')', and NO open '('
    
  2. How to utilized stack.

     a. Empty stack + ')', means leading ')'
     b. Non-Empty stack + the end, means open '('
    

An Example

Problems

Problems Possible Solutions Key Points Code Comments
20. Valid Parentheses KP in code type 1
1249. Minimum Remove to Make Valid Parentheses Stack code
32. Longest Valid Parentheses stack
Dynamic Programming
type 1
921. Minimum Add to Make Parentheses Valid KP in code type 1
1541. Minimum Insertions to Balance a Parentheses String type 1
TODO: Questions in the code.
22. Generate Parentheses KP in code backtrack type 2 -

References

  1. https://labuladong.online/algo/frequency-interview/bracket-problems-summary/
  2. https://labuladong.online/algo/essential-technique/backtrack-framework/
  3. https://labuladong.online/algo/practice-in-action/generate-parentheses/

results matching ""

    No results matching ""