You are given a string s that contains some bracket pairs, with each pair containing a non-empty key.
"(name)is(age)yearsold", there are two bracket pairs that contain the keys "name" and "age".You know the values of a wide range of keys. This is represented by a 2D string array knowledge where each knowledge[i] = [keyi, valuei] indicates that key keyi has a value of valuei.
You are tasked to evaluate all of the bracket pairs. When you evaluate a bracket pair that contains some key keyi, you will:
keyi and the bracket pair with the key's corresponding valuei.keyi and the bracket pair with a question mark "?" (without the quotation marks).Each key will appear at most once in your knowledge. There will not be any nested brackets in s.
Return the resulting string after evaluating all of the bracket pairs.
Example 1:
Input: s = "(name)is(age)yearsold", knowledge = [["name","bob"],["age","two"]] Output: "bobistwoyearsold" Explanation: The key "name" has a value of "bob", so replace "(name)" with "bob". The key "age" has a value of "two", so replace "(age)" with "two".
Example 2:
Input: s = "hi(name)", knowledge = [["a","b"]] Output: "hi?" Explanation: As you do not know the value of the key "name", replace "(name)" with "?".
Example 3:
Input: s = "(a)(a)(a)aaa", knowledge = [["a","yes"]] Output: "yesyesyesaaa" Explanation: The same key can appear multiple times. The key "a" has a value of "yes", so replace all occurrences of "(a)" with "yes". Notice that the "a"s not in a bracket pair are not evaluated.
Constraints:
1 <= s.length <= 1050 <= knowledge.length <= 105knowledge[i].length == 21 <= keyi.length, valuei.length <= 10s consists of lowercase English letters and round brackets '(' and ')'.'(' in s will have a corresponding close bracket ')'.s will be non-empty.s.keyi and valuei consist of lowercase English letters.keyi in knowledge is unique.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:
The brute force approach to this problem means trying out every possible combination. We'll go through each bracketed expression and try to find its value by looking through all the possible key-value pairs until we find the right one or determine that it's missing.
Here's how the algorithm would work step-by-step:
def evaluate_bracket_pairs_brute_force(input_string, knowledge):
result = ''
i = 0
while i < len(input_string):
if input_string[i] == '(':
# Found an opening bracket, start extracting the key
start_index = i + 1
end_index = i + 1
while end_index < len(input_string) and input_string[end_index] != ')':
end_index += 1
key = input_string[start_index:end_index]
found_value = False
for key_value_pair in knowledge:
if key_value_pair[0] == key:
result += key_value_pair[1]
found_value = True
break
# If no matching key is found, append '?'
if not found_value:
result += '?'
i = end_index + 1
else:
result += input_string[i]
i += 1
return resultThis problem involves substituting parts of a string enclosed in parentheses with values from a provided lookup table. The most efficient approach is to simply read through the original string once, checking for the parentheses along the way. This allows us to build the result in a single pass.
Here's how the algorithm would work step-by-step:
def evaluate_bracket_pairs(input_string, knowledge_pairs):
result = ""
index = 0
while index < len(input_string):
if input_string[index] == '(':
# Found an open parenthesis, start extracting the key.
index += 1
key_start_index = index
while index < len(input_string) and input_string[index] != ')':
index += 1
key = input_string[key_start_index:index]
# Look up the key in the knowledge pairs.
if key in knowledge_pairs:
result += knowledge_pairs[key]
else:
result += "?"
else:
# Append the current character to the result.
result += input_string[index]
index += 1
return result| Case | How to Handle |
|---|---|
| Null or empty input string | Return an empty string immediately as there's nothing to evaluate. |
| Null or empty knowledge dictionary | Treat all bracket pairs as unresolved, resulting in their removal from the output string. |
| Input string contains unbalanced brackets (e.g., only opening brackets) | Ignore any unmatched opening bracket and continue processing the rest of the string. |
| Nested bracket pairs (e.g., (a(b)c)) | Handle only the outermost matching brackets, as the problem description only requests evaluation of bracket pairs, not nested structures. |
| Knowledge dictionary contains null or empty values | Treat null/empty values as valid substitutions, effectively replacing the bracketed key with an empty string. |
| Knowledge dictionary key doesn't exist | Replace the unresolved bracket pair with a question mark '?' as specified in the problem description. |
| Very long input string or large knowledge dictionary | Ensure the chosen approach (e.g., using StringBuilder and HashMap) scales efficiently to avoid performance bottlenecks. |
| Bracket keys containing special characters, including other brackets | The solution should correctly identify the keys based on the specified bracket delimiters and handle any internal special characters as part of the key. |