34 lines
1.1 KiB
Markdown
34 lines
1.1 KiB
Markdown
# The Parentheses Balancing Problem (RIP Exercise)
|
|
|
|
## What is this exercise about?
|
|
|
|
Imagine you have a string of parentheses like this: `"(()"`.
|
|
|
|
Think of parentheses like **doors** in a building:
|
|
- `(` is like **opening a door**
|
|
- `)` is like **closing a door**
|
|
|
|
For everything to work properly, every door you open must have a matching door that closes it!
|
|
|
|
## The Problem
|
|
|
|
Sometimes we get strings where the doors don't match properly. For example:
|
|
- `"((("` - We opened 3 doors but never closed any!
|
|
- `"())"` - We opened 1 door, closed 1 door, but then tried to close another door that was never opened!
|
|
|
|
Our job is to **fix** these strings by removing the minimum number of parentheses (but replacing them with spaces instead of deleting them completely).
|
|
|
|
## Real-World Example
|
|
|
|
Let's say you have: `"(()"`
|
|
|
|
This means:
|
|
1. Open door 1: `(`
|
|
2. Open door 2: `(`
|
|
3. Close door 2: `)`
|
|
4. But door 1 is still open!
|
|
|
|
To fix this, we need to either:
|
|
- Remove one of the opening doors: ` ()` (remove first `(`)
|
|
- Or remove one of the opening doors: `( )` (remove second `(`)
|