java.lang.Object
g1801_1900.s1807_evaluate_the_bracket_pairs_of_a_string.Solution

public class Solution extends Object
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>&quot;(name)is(age)yearsold&quot;</code>, there are <strong>two</strong> bracket pairs that contain the keys <code>&quot;name&quot;</code> and <code>&quot;age&quot;</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&rsquo;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>&quot;?&quot;</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 = &ldquo;(name)is(age)yearsold&rdquo;, knowledge = [[&ldquo;name&rdquo;,&ldquo;bob&rdquo;],[&ldquo;age&rdquo;,&ldquo;two&rdquo;]]</p> <p><strong>Output:</strong> &ldquo;bobistwoyearsold&rdquo;</p> <p><strong>Explanation:</strong></p> <p>The key &ldquo;name&rdquo; has a value of &ldquo;bob&rdquo;, so replace &ldquo;(name)&rdquo; with &ldquo;bob&rdquo;.</p> <p>The key &ldquo;age&rdquo; has a value of &ldquo;two&rdquo;, so replace &ldquo;(age)&rdquo; with &ldquo;two&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;hi(name)&rdquo;, knowledge = [[&ldquo;a&rdquo;,&ldquo;b&rdquo;]]</p> <p><strong>Output:</strong> &ldquo;hi?&rdquo;</p> <p><strong>Explanation:</strong> As you do not know the value of the key &ldquo;name&rdquo;, replace &ldquo;(name)&rdquo; with &ldquo;?&rdquo;.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;(a)(a)(a)aaa&rdquo;, knowledge = [[&ldquo;a&rdquo;,&ldquo;yes&rdquo;]]</p> <p><strong>Output:</strong> &ldquo;yesyesyesaaa&rdquo;</p> <p><strong>Explanation:</strong> The same key can appear multiple times.</p> <p>The key &ldquo;a&rdquo; has a value of &ldquo;yes&rdquo;, so replace all occurrences of &ldquo;(a)&rdquo; with &ldquo;yes&rdquo;.</p> <p>Notice that the &quot;a&quot;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 Details

    • Solution

      public Solution()
  • Method Details