Evaluate the Bracket Pairs of a String - Problem

You are given a string s that contains some bracket pairs, with each pair containing a non-empty key.

For example, in the string "(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:

  • Replace keyi and the bracket pair with the key's corresponding valuei.
  • If you do not know the value of the key, you will replace keyi and the bracket pair with a question mark "?".

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.

Input & Output

Example 1 — Basic Replacement
$ Input: s = "(name)is(age)yearsold", knowledge = [["name","Bob"],["age","20"]]
Output: "Bobis20yearsold"
💡 Note: The key "name" is replaced with "Bob" and "age" is replaced with "20", resulting in "Bobis20yearsold"
Example 2 — Unknown Key
$ Input: s = "hi(name)", knowledge = [["age","25"]]
Output: "hi?"
💡 Note: The key "name" is not found in knowledge, so it's replaced with "?"
Example 3 — No Brackets
$ Input: s = "hello", knowledge = [["name","Bob"]]
Output: "hello"
💡 Note: No bracket pairs found, so the string remains unchanged

Constraints

  • 1 ≤ s.length ≤ 105
  • 0 ≤ knowledge.length ≤ 105
  • knowledge[i].length == 2

Visualization

Tap to expand
Evaluate Bracket Pairs of a String INPUT String s: "(name)is(age)yearsold" (name) is (age) yearsold Knowledge Array: Key Value "name" "Bob" "age" "20" Build HashMap: HashMap "name" --> "Bob" "age" --> "20" O(1) lookup time ALGORITHM STEPS 1 Build HashMap Convert knowledge to map 2 Parse String Find ( and ) brackets 3 Extract Keys Get text between brackets 4 Replace Values Lookup and substitute Processing Flow: "(name)is(age)yearsold" lookup "name" --> "Bob" "Bob" + "is" + "(age)yearsold" lookup "age" --> "20" "Bob" + "is" + "20" + "yearsold" Concatenate result FINAL RESULT Output String: "Bobis20yearsold" Breakdown: Bob is 20 yearsold OK - Complete! Time: O(n + m) Space: O(m) n=string, m=knowledge Key Insight: Hash Map Optimization By converting the knowledge array into a HashMap first, we achieve O(1) lookup time for each key. This avoids O(n*m) nested loops. We parse the string once, extracting keys between brackets and replacing them with values (or "?" if not found). StringBuilder ensures efficient string building. TutorialsPoint - Evaluate the Bracket Pairs of a String | Hash Map Optimization Approach
Asked in
Facebook 15 Amazon 12 Microsoft 8
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen