Given a string s containing only three types of characters: '(', ')' and '*', return true if s is valid.
The following rules define a valid string:
'(' must have a corresponding right parenthesis ')'.')' must have a corresponding left parenthesis '('.'(' must go before the corresponding right parenthesis ')'.'*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string "".Example 1:
Input: s = "()" Output: true
Example 2:
Input: s = "(*)" Output: true
Example 3:
Input: s = "(*))" Output: true
Constraints:
1 <= s.length <= 100s[i] is '(', ')' or '*'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a puzzle containing standard opening and closing parentheses, along with wildcards that can pretend to be either one or nothing at all. The brute force strategy involves testing every single possible identity for every wildcard to see if any combination creates a perfectly balanced set of parentheses.
Here's how the algorithm would work step-by-step:
class Solution:
def checkValidString(self, input_string: str) -> bool:
def evaluate_paths(string_index, open_count):
# Fail early if closing brackets exceed available opening brackets to prune invalid paths
if open_count < 0:
return False
if string_index == len(input_string):
return open_count == 0
current_char = input_string[string_index]
if current_char == '(':
return evaluate_paths(string_index + 1, open_count + 1)
elif current_char == ')':
return evaluate_paths(string_index + 1, open_count - 1)
else:
# Branch into three possibilities for the wildcard to explore all valid combinations
return (
evaluate_paths(string_index + 1, open_count + 1) or
evaluate_paths(string_index + 1, open_count - 1) or
evaluate_paths(string_index + 1, open_count)
)
return evaluate_paths(0, 0)Instead of guessing every possible role for the wildcards, we can track a moving range of how many open brackets are currently allowed. By constantly updating the minimum and maximum possible number of open brackets as we read through the text, we can determine if a valid balance is achievable at the end.
Here's how the algorithm would work step-by-step:
def check_valid_string(input_string): minimum_open_brackets = 0 maximum_open_brackets = 0 for current_character in input_string: if current_character == '(': minimum_open_brackets += 1 maximum_open_brackets += 1 elif current_character == ')': minimum_open_brackets -= 1 maximum_open_brackets -= 1 else: minimum_open_brackets -= 1 maximum_open_brackets += 1 # Too many closing brackets encountered, impossible to recover balance. if maximum_open_brackets < 0: return False # Minimum cannot drop below zero since we cannot have negative open brackets. if minimum_open_brackets < 0: minimum_open_brackets = 0 # Ensure zero is safely inside the final range to confirm full closure. return minimum_open_brackets == 0| Case | How to Handle |
|---|---|
| Empty string input | An empty string is vacuously valid and should return true immediately. |
| Single character string containing only an asterisk | The asterisk can act as an empty string, allowing the function to return true. |
| String with a single unmatched parenthesis like '(' or ')' | Tracking minimum and maximum open parentheses counts ensures single unmatched characters correctly evaluate to false. |
| String starting with a closing parenthesis such as ')(' | The minimum open count must never drop below zero, immediately invalidating strings that start with a closing parenthesis. |
| String with unbalanced extra opening parentheses like '((*' | At the end of the traversal, checking that the minimum open count reaches zero ensures no unmatched opening parentheses remain. |
| Extremely long strings of up to one hundred thousand characters | An O(n) greedy approach using constant extra space allows the solution to scale efficiently without stack overflow issues. |
| Strings with heavily skewed distributions consisting entirely of asterisks | Treating asterisks flexibly as empty strings allows the balance range to safely span from zero to the total asterisk count. |