Decide Whether a Parenthesis String With Star Wildcards Can Be Balanced
Company: Oracle
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Decide whether a string of parentheses and stars can become a balanced parenthesis string when each star may independently act as an opening parenthesis, a closing parenthesis or an empty string. Tests reasoning over many possible replacements at once and edge cases with unmatched parentheses near either end.
Constraints
- 1 <= len(s) <= 100
- Every character of s is '(', ')' or '*'.
Examples
Input: ('(',)
Expected Output: False
Explanation: A lone '(' has nothing after it that could close it.
Input: (')',)
Expected Output: False
Explanation: A lone ')' has nothing before it that could open a pair.
Hints
- The balanced condition has two parts: a condition on every prefix while reading left to right, and an equality of counts at the end. A replacement must satisfy both.
- An unmatched '(' can only be closed by a ')' or a '*' that comes after it; an unmatched ')' can only be opened by a '(' or a '*' that comes before it.
- A '*' may also vanish, so a star is never forced to create an extra bracket.