Class Solution
java.lang.Object
g1801_1900.s1807_evaluate_the_bracket_pairs_of_a_string.Solution
1807 - Evaluate the Bracket Pairs of a String.<p>Medium</p>
<p>You are given a string <code>s</code> that contains some bracket pairs, with each pair containing a <strong>non-empty</strong> key.</p>
<ul>
<li>For example, in the string <code>"(name)is(age)yearsold"</code>, there are <strong>two</strong> bracket pairs that contain the keys <code>"name"</code> and <code>"age"</code>.</li>
</ul>
<p>You know the values of a wide range of keys. This is represented by a 2D string array <code>knowledge</code> where each <code>knowledge[i] = [key<sub>i</sub>, value<sub>i</sub>]</code> indicates that key <code>key<sub>i</sub></code> has a value of <code>value<sub>i</sub></code>.</p>
<p>You are tasked to evaluate <strong>all</strong> of the bracket pairs. When you evaluate a bracket pair that contains some key <code>key<sub>i</sub></code>, you will:</p>
<ul>
<li>Replace <code>key<sub>i</sub></code> and the bracket pair with the key’s corresponding <code>value<sub>i</sub></code>.</li>
<li>If you do not know the value of the key, you will replace <code>key<sub>i</sub></code> and the bracket pair with a question mark <code>"?"</code> (without the quotation marks).</li>
</ul>
<p>Each key will appear at most once in your <code>knowledge</code>. There will not be any nested brackets in <code>s</code>.</p>
<p>Return <em>the resulting string after evaluating <strong>all</strong> of the bracket pairs.</em></p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> s = “(name)is(age)yearsold”, knowledge = [[“name”,“bob”],[“age”,“two”]]</p>
<p><strong>Output:</strong> “bobistwoyearsold”</p>
<p><strong>Explanation:</strong></p>
<p>The key “name” has a value of “bob”, so replace “(name)” with “bob”.</p>
<p>The key “age” has a value of “two”, so replace “(age)” with “two”.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> s = “hi(name)”, knowledge = [[“a”,“b”]]</p>
<p><strong>Output:</strong> “hi?”</p>
<p><strong>Explanation:</strong> As you do not know the value of the key “name”, replace “(name)” with “?”.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> s = “(a)(a)(a)aaa”, knowledge = [[“a”,“yes”]]</p>
<p><strong>Output:</strong> “yesyesyesaaa”</p>
<p><strong>Explanation:</strong> The same key can appear multiple times.</p>
<p>The key “a” has a value of “yes”, so replace all occurrences of “(a)” with “yes”.</p>
<p>Notice that the "a"s not in a bracket pair are not evaluated.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>0 <= knowledge.length <= 10<sup>5</sup></code></li>
<li><code>knowledge[i].length == 2</code></li>
<li><code>1 <= key<sub>i</sub>.length, value<sub>i</sub>.length <= 10</code></li>
<li><code>s</code> consists of lowercase English letters and round brackets <code>'('</code> and <code>')'</code>.</li>
<li>Every open bracket <code>'('</code> in <code>s</code> will have a corresponding close bracket <code>')'</code>.</li>
<li>The key in each bracket pair of <code>s</code> will be non-empty.</li>
<li>There will not be any nested bracket pairs in <code>s</code>.</li>
<li><code>key<sub>i</sub></code> and <code>value<sub>i</sub></code> consist of lowercase English letters.</li>
<li>Each <code>key<sub>i</sub></code> in <code>knowledge</code> is unique.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
evaluate
-