{"id":8287,"date":"2021-03-27T22:25:37","date_gmt":"2021-03-28T05:25:37","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8287"},"modified":"2021-03-27T22:27:36","modified_gmt":"2021-03-28T05:27:36","slug":"leetcode-1807-evaluate-the-bracket-pairs-of-a-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1807-evaluate-the-bracket-pairs-of-a-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1807. Evaluate the Bracket Pairs of a String"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>s<\/code>&nbsp;that contains some bracket pairs, with each pair containing a&nbsp;<strong>non-empty<\/strong>&nbsp;key.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, in the string&nbsp;<code>\"(name)is(age)yearsold\"<\/code>, there are&nbsp;<strong>two<\/strong>&nbsp;bracket pairs that contain the keys&nbsp;<code>\"name\"<\/code>&nbsp;and&nbsp;<code>\"age\"<\/code>.<\/li><\/ul>\n\n\n\n<p>You know the values of a wide range of keys. This is represented by a 2D string array&nbsp;<code>knowledge<\/code>&nbsp;where each&nbsp;<code>knowledge[i] = [key<sub>i<\/sub>, value<sub>i<\/sub>]<\/code>&nbsp;indicates that key&nbsp;<code>key<sub>i<\/sub><\/code>&nbsp;has a value of&nbsp;<code>value<sub>i<\/sub><\/code>.<\/p>\n\n\n\n<p>You are tasked to evaluate&nbsp;<strong>all<\/strong>&nbsp;of the bracket pairs. When you evaluate a bracket pair that contains some key&nbsp;<code>key<sub>i<\/sub><\/code>, you will:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Replace&nbsp;<code>key<sub>i<\/sub><\/code>&nbsp;and the bracket pair with the key&#8217;s corresponding&nbsp;<code>value<sub>i<\/sub><\/code>.<\/li><li>If you do not know the value of the key, you will replace&nbsp;<code>key<sub>i<\/sub><\/code>&nbsp;and the bracket pair with a question mark&nbsp;<code>\"?\"<\/code>&nbsp;(without the quotation marks).<\/li><\/ul>\n\n\n\n<p>Each key will appear at most once in your&nbsp;<code>knowledge<\/code>. There will not be any nested brackets in&nbsp;<code>s<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the resulting string after evaluating&nbsp;<strong>all<\/strong>&nbsp;of the bracket pairs.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"(name)is(age)yearsold\", knowledge = [[\"name\",\"bob\"],[\"age\",\"two\"]]\n<strong>Output:<\/strong> \"bobistwoyearsold\"\n<strong>Explanation:<\/strong>\nThe key \"name\" has a value of \"bob\", so replace \"(name)\" with \"bob\".\nThe key \"age\" has a value of \"two\", so replace \"(age)\" with \"two\".\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"hi(name)\", knowledge = [[\"a\",\"b\"]]\n<strong>Output:<\/strong> \"hi?\"\n<strong>Explanation:<\/strong> As you do not know the value of the key \"name\", replace \"(name)\" with \"?\".\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"(a)(a)(a)aaa\", knowledge = [[\"a\",\"yes\"]]\n<strong>Output:<\/strong> \"yesyesyesaaa\"\n<strong>Explanation:<\/strong> The same key can appear multiple times.\nThe key \"a\" has a value of \"yes\", so replace all occurrences of \"(a)\" with \"yes\".\nNotice that the \"a\"s not in a bracket pair are not evaluated.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"(a)(b)\", knowledge = [[\"a\",\"b\"],[\"b\",\"a\"]]\n<strong>Output:<\/strong> \"ba\"<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= knowledge.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>knowledge[i].length == 2<\/code><\/li><li><code>1 &lt;= key<sub>i<\/sub>.length, value<sub>i<\/sub>.length &lt;= 10<\/code><\/li><li><code>s<\/code>&nbsp;consists of lowercase English letters and round brackets&nbsp;<code>'('<\/code>&nbsp;and&nbsp;<code>')'<\/code>.<\/li><li>Every open bracket&nbsp;<code>'('<\/code>&nbsp;in&nbsp;<code>s<\/code>&nbsp;will have a corresponding close bracket&nbsp;<code>')'<\/code>.<\/li><li>The key in each bracket pair of&nbsp;<code>s<\/code>&nbsp;will be non-empty.<\/li><li>There will not be any nested bracket pairs in&nbsp;<code>s<\/code>.<\/li><li><code>key<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>value<sub>i<\/sub><\/code>&nbsp;consist of lowercase English letters.<\/li><li>Each&nbsp;<code>key<sub>i<\/sub><\/code>&nbsp;in&nbsp;<code>knowledge<\/code>&nbsp;is unique.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable + Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n+k)<br>Space complexity: O(n+k)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string evaluate(string s, vector<vector<string>>& knowledge) {\n    unordered_map<string, string> m;\n    for (const auto& p : knowledge)\n      m[p[0]] = p[1];\n    string ans;\n    string cur;\n    bool in = false;\n    for (char c : s) {\n      if (c == '(') {\n        in = true;       \n      } else if (c == ')') {\n        if (m.count(cur))\n          ans += m[cur];\n        else\n          ans += \"?\";\n        cur.clear();\n        in = false;      \n      } else {\n        if (!in) ans += c;\n        else cur += c;\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;s&nbsp;that contains some bracket pairs, with each pair containing a&nbsp;non-empty&nbsp;key. For example, in the string&nbsp;&#8220;(name)is(age)yearsold&#8221;, there are&nbsp;two&nbsp;bracket pairs that contain the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,177,179,4],"class_list":["post-8287","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-medium","tag-simulation","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8287"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8287\/revisions"}],"predecessor-version":[{"id":8289,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8287\/revisions\/8289"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}