{"id":2934,"date":"2018-06-26T21:16:13","date_gmt":"2018-06-27T04:16:13","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2934"},"modified":"2018-06-27T20:28:37","modified_gmt":"2018-06-28T03:28:37","slug":"leetcode-856-score-of-parentheses","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-856-score-of-parentheses\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 856. Score of Parentheses"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 856. Score of Parentheses - \u5237\u9898\u627e\u5de5\u4f5c EP198\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/tiAaVfMcL9w?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<p>Given a balanced parentheses string\u00a0<code>S<\/code>, compute the score of the string based on the following rule:<\/p>\n<ul>\n<li><code>()<\/code>\u00a0has score 1<\/li>\n<li><code>AB<\/code>\u00a0has score\u00a0<code>A + B<\/code>, where A and B are balanced parentheses strings.<\/li>\n<li><code>(A)<\/code>\u00a0has score\u00a0<code>2 * A<\/code>, where A is a balanced parentheses string.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<div>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">\"()\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">1<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">\"(())\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">2<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-3-1\">\"()()\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">2<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 4:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-4-1\">\"(()(()))\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-4\">6<\/span>\r\n<\/pre>\n<h1><\/h1>\n<h1><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2941\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/856-ep198.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/856-ep198.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/856-ep198-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/856-ep198-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/h1>\n<h1><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2940\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/856-ep198-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/856-ep198-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/856-ep198-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/06\/856-ep198-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/h1>\n<h1>Solution1: Recursion<\/h1>\n<p>Time complexity: O(n^2)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 4ms\r\nclass Solution {\r\npublic:\r\n  int scoreOfParentheses(string S) {\r\n    return score(S, 0, S.length() - 1);\r\n  }\r\nprivate:\r\n  int score(const string&amp; S, int l, int r) {    \r\n    if (r - l == 1) return 1; \/\/ \"()\"\r\n    int b = 0;\r\n    for (int i = l; i &lt; r; ++i) {\r\n      if (S[i] == '(') ++b;\r\n      if (S[i] == ')') --b;\r\n      if (b == 0) \/\/ balanced\r\n        \/\/ score(\"(A)(B)\") = score(\"(A)\") + score(\"(B)\")\r\n        return score(S, l, i) + score(S, i + 1, r);    \r\n    }\r\n    \/\/ score(\"(A)\") = 2 * score(\"A\")\r\n    return 2 * score(S, l + 1, r - 1); \r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<h1><strong>Solution2: Counting<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int scoreOfParentheses(string S) {\r\n    int ans = 0;\r\n    int d = -1;\r\n    for (int i = 0; i &lt; S.length(); ++i) {\r\n      d += S[i] == '(' ? 1 : -1;\r\n      if (S[i] == '(' &amp;&amp; S[i + 1] == ')')\r\n        ans += 1 &lt;&lt; d;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a balanced parentheses string\u00a0S, compute the score of the string based on the following rule: ()\u00a0has score 1 AB\u00a0has score\u00a0A + B, where&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[312,313,180,4],"class_list":["post-2934","post","type-post","status-publish","format-standard","hentry","category-string","tag-balance","tag-divide-and-conquer","tag-stack","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2934","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=2934"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2934\/revisions"}],"predecessor-version":[{"id":2943,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2934\/revisions\/2943"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}