|
| 1 | +--- |
| 2 | +id: evaluate-the-bracket-pairs-of-a-string |
| 3 | +title: Evaluate the Bracket Pairs of a String |
| 4 | +sidebar_label: 1807-Evaluate the Bracket Pairs of a String |
| 5 | +tags: |
| 6 | + - String Manipulation |
| 7 | + - Hash Table |
| 8 | + - LeetCode |
| 9 | + - Java |
| 10 | + - Python |
| 11 | + - C++ |
| 12 | +description: "This is a solution to the Evaluate Bracket Pairs problem on LeetCode." |
| 13 | +sidebar_position: 30 |
| 14 | +--- |
| 15 | + |
| 16 | +## Problem Description |
| 17 | + |
| 18 | +You are given a string `s` that contains some bracket pairs, with each pair containing a non-empty key. |
| 19 | + |
| 20 | +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`. |
| 21 | + |
| 22 | +You are tasked to evaluate all of the bracket pairs. When you evaluate a bracket pair that contains some key `keyi`, you will: |
| 23 | +- Replace `keyi` and the bracket pair with the key's corresponding `valuei`. |
| 24 | +- If you do not know the value of the key, you will replace `keyi` and the bracket pair with a question mark "?" (without the quotation marks). |
| 25 | + |
| 26 | +Each key will appear at most once in your knowledge. There will not be any nested brackets in `s`. |
| 27 | + |
| 28 | +Return the resulting string after evaluating all of the bracket pairs. |
| 29 | + |
| 30 | +### Examples |
| 31 | + |
| 32 | +**Example 1:** |
| 33 | + |
| 34 | +``` |
| 35 | +Input: s = "(name)is(age)yearsold", knowledge = [["name","bob"],["age","two"]] |
| 36 | +Output: "bobistwoyearsold" |
| 37 | +Explanation: |
| 38 | +The key "name" has a value of "bob", so replace "(name)" with "bob". |
| 39 | +The key "age" has a value of "two", so replace "(age)" with "two". |
| 40 | +``` |
| 41 | + |
| 42 | +**Example 2:** |
| 43 | + |
| 44 | +``` |
| 45 | +Input: s = "hi(name)", knowledge = [["a","b"]] |
| 46 | +Output: "hi?" |
| 47 | +Explanation: As you do not know the value of the key "name", replace "(name)" with "?". |
| 48 | +``` |
| 49 | + |
| 50 | +**Example 3:** |
| 51 | + |
| 52 | +``` |
| 53 | +Input: s = "(a)(a)(a)aaa", knowledge = [["a","yes"]] |
| 54 | +Output: "yesyesyesaaa" |
| 55 | +Explanation: The same key can appear multiple times. |
| 56 | +The key "a" has a value of "yes", so replace all occurrences of "(a)" with "yes". |
| 57 | +Notice that the "a"s not in a bracket pair are not evaluated. |
| 58 | +``` |
| 59 | + |
| 60 | +### Constraints |
| 61 | + |
| 62 | +- `1 <= s.length <= 10^5` |
| 63 | +- `0 <= knowledge.length <= 10^5` |
| 64 | +- `knowledge[i].length == 2` |
| 65 | +- `1 <= keyi.length, valuei.length <= 10` |
| 66 | +- `s` consists of lowercase English letters and round brackets '(' and ')'. |
| 67 | +- Every open bracket '(' in `s` will have a corresponding close bracket ')'. |
| 68 | +- The key in each bracket pair of `s` will be non-empty. |
| 69 | +- There will not be any nested bracket pairs in `s`. |
| 70 | +- `keyi` and `valuei` consist of lowercase English letters. |
| 71 | +- Each `keyi` in knowledge is unique. |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +## Solution for Evaluate Bracket Pairs Problem |
| 76 | + |
| 77 | +To solve this problem, we need to use a hash map to store the key-value pairs from the knowledge array. Then, we can iterate through the string `s` and build the resulting string by replacing the keys within brackets with their corresponding values from the hash map. |
| 78 | + |
| 79 | +### Approach |
| 80 | + |
| 81 | +1. Create a hash map from the knowledge array. |
| 82 | +2. Initialize a result string. |
| 83 | +3. Traverse the string `s`: |
| 84 | + - If we encounter a '(', start collecting the key until we find a ')'. |
| 85 | + - Replace the key with its value from the hash map, or '?' if the key is not found. |
| 86 | +4. Continue until the entire string is processed. |
| 87 | + |
| 88 | +### Code in Different Languages |
| 89 | + |
| 90 | +<Tabs> |
| 91 | +<TabItem value="C++" label="C++" default> |
| 92 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 93 | + |
| 94 | +```cpp |
| 95 | +class Solution { |
| 96 | +public: |
| 97 | + string evaluate(string s, vector<vector<string>>& knowledge) { |
| 98 | + unordered_map<string, string> map; |
| 99 | + for (auto& pair : knowledge) { |
| 100 | + map[pair[0]] = pair[1]; |
| 101 | + } |
| 102 | + |
| 103 | + string result; |
| 104 | + int i = 0; |
| 105 | + while (i < s.size()) { |
| 106 | + if (s[i] == '(') { |
| 107 | + int j = i + 1; |
| 108 | + while (s[j] != ')') j++; |
| 109 | + string key = s.substr(i + 1, j - i - 1); |
| 110 | + result += map.count(key) ? map[key] : "?"; |
| 111 | + i = j + 1; |
| 112 | + } else { |
| 113 | + result += s[i++]; |
| 114 | + } |
| 115 | + } |
| 116 | + return result; |
| 117 | + } |
| 118 | +}; |
| 119 | +``` |
| 120 | +
|
| 121 | +</TabItem> |
| 122 | +<TabItem value="Java" label="Java"> |
| 123 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 124 | +
|
| 125 | +```java |
| 126 | +class Solution { |
| 127 | + public String evaluate(String s, List<List<String>> knowledge) { |
| 128 | + Map<String, String> map = new HashMap<>(); |
| 129 | + for (List<String> pair : knowledge) { |
| 130 | + map.put(pair.get(0), pair.get(1)); |
| 131 | + } |
| 132 | + |
| 133 | + StringBuilder result = new StringBuilder(); |
| 134 | + int i = 0; |
| 135 | + while (i < s.length()) { |
| 136 | + if (s.charAt(i) == '(') { |
| 137 | + int j = i + 1; |
| 138 | + while (s.charAt(j) != ')') j++; |
| 139 | + String key = s.substring(i + 1, j); |
| 140 | + result.append(map.getOrDefault(key, "?")); |
| 141 | + i = j + 1; |
| 142 | + } else { |
| 143 | + result.append(s.charAt(i++)); |
| 144 | + } |
| 145 | + } |
| 146 | + return result.toString(); |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +</TabItem> |
| 152 | +<TabItem value="Python" label="Python"> |
| 153 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 154 | + |
| 155 | +```python |
| 156 | +class Solution: |
| 157 | + def evaluate(self, s: str, knowledge: List[List[str]]) -> str: |
| 158 | + knowledge_dict = {key: value for key, value in knowledge} |
| 159 | + result = [] |
| 160 | + i = 0 |
| 161 | + while i < len(s): |
| 162 | + if s[i] == '(': |
| 163 | + j = i + 1 |
| 164 | + while s[j] != ')': |
| 165 | + j += 1 |
| 166 | + key = s[i + 1:j] |
| 167 | + result.append(knowledge_dict.get(key, "?")) |
| 168 | + i = j + 1 |
| 169 | + else: |
| 170 | + result.append(s[i]) |
| 171 | + i += 1 |
| 172 | + return ''.join(result) |
| 173 | +``` |
| 174 | + |
| 175 | +</TabItem> |
| 176 | +</Tabs> |
| 177 | + |
| 178 | +#### Complexity Analysis |
| 179 | + |
| 180 | +- **Time Complexity**: $O(n + m)$, where `n` is the length of the string `s` and `m` is the number of key-value pairs in `knowledge`. |
| 181 | +- **Space Complexity**: $O(m)$, for storing the hash map of knowledge. |
| 182 | + |
| 183 | +--- |
| 184 | + |
| 185 | +<h2>Authors:</h2> |
| 186 | + |
| 187 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 188 | +{['ImmidiSivani'].map(username => ( |
| 189 | + <Author key={username} username={username} /> |
| 190 | +))} |
| 191 | +</div> |
| 192 | +``` |
0 commit comments