easy +10 pts

Match Balanced Parentheses with Regex

Use Python's re module to check if parentheses in a string are balanced.

Complete the function `is_balanced(text: str) -> bool` so that it returns `True` if the parentheses `(` and `)` in `text` are balanced and properly nested, and `False` otherwise. Use Python's `re` module as part of your logic. The function should ignore all characters except parentheses. A string with no parentheses at all is considered balanced. **Examples:** - `is_balanced("((a+b)*c)")` → `True` - `is_balanced(")(")` → `False` - `is_balanced("(a")` → `False` - `is_balanced("abc")` → `True` **Implementation notes:** - Your function must use `re` (regex) operations at least once. For instance, use `re.findall` or `re.sub` to extract or process parentheses. - You may use additional logic if needed, but relying solely on a regex pattern is allowed. - Do not use built-in string methods like `.count()` to count parentheses directly; use regex to extract them. Constraints: - `text` will be a string of length 0 to 10,000. - Only ASCII characters. - Time complexity must be O(n) or better.

Constraints

Input: `text` is a string of length 0 to 10,000 characters. The function should run in O(n) time and use O(n) space (or less).

Example

```python
>>> is_balanced("((a+b)*c)")
True
>>> is_balanced(")(")
False
>>> is_balanced("(a")
False
>>> is_balanced("abc")
True
```
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Extract all parentheses using a regex like `re.findall(r'[()]', text)`.
You can iterate through the extracted characters and maintain a counter.
If the counter ever goes negative, parentheses are unbalanced.
At the end, the counter must be exactly 0.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.