{"id":7765,"date":"2020-12-05T21:13:07","date_gmt":"2020-12-06T05:13:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7765"},"modified":"2020-12-05T21:13:26","modified_gmt":"2020-12-06T05:13:26","slug":"leetcode-1678-goal-parser-interpretation","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1678-goal-parser-interpretation\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1678. Goal Parser Interpretation"},"content":{"rendered":"\n<p>You own a&nbsp;<strong>Goal Parser<\/strong>&nbsp;that can interpret a string&nbsp;<code>command<\/code>. The&nbsp;<code>command<\/code>&nbsp;consists of an alphabet of&nbsp;<code>\"G\"<\/code>,&nbsp;<code>\"()\"<\/code>&nbsp;and\/or&nbsp;<code>\"(al)\"<\/code>&nbsp;in some order. The Goal Parser will interpret&nbsp;<code>\"G\"<\/code>&nbsp;as the string&nbsp;<code>\"G\"<\/code>,&nbsp;<code>\"()\"<\/code>&nbsp;as the string&nbsp;<code>\"o\"<\/code>, and&nbsp;<code>\"(al)\"<\/code>&nbsp;as the string&nbsp;<code>\"al\"<\/code>. The interpreted strings are then concatenated in the original order.<\/p>\n\n\n\n<p>Given the string&nbsp;<code>command<\/code>, return&nbsp;<em>the&nbsp;<strong>Goal Parser<\/strong>&#8216;s interpretation of&nbsp;<\/em><code>command<\/code>.<\/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> command = \"G()(al)\"\n<strong>Output:<\/strong> \"Goal\"\n<strong>Explanation:<\/strong>&nbsp;The Goal Parser interprets the command as follows:\nG -&gt; G\n() -&gt; o\n(al) -&gt; al\nThe final concatenated result is \"Goal\".\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> command = \"G()()()()(al)\"\n<strong>Output:<\/strong> \"Gooooal\"\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> command = \"(al)G(al)()()G\"\n<strong>Output:<\/strong> \"alGalooG\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= command.length &lt;= 100<\/code><\/li><li><code>command<\/code>&nbsp;consists of&nbsp;<code>\"G\"<\/code>,&nbsp;<code>\"()\"<\/code>, and\/or&nbsp;<code>\"(al)\"<\/code>&nbsp;in some order.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: String<\/strong><\/h2>\n\n\n\n<p>If we encounter &#8216;(&#8216; check the next character to determine whether it&#8217;s &#8216;()&#8217; or &#8216;(al&#8217;)<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  string interpret(string command) {\n    string ans;\n    for (int i = 0; i < command.size(); ++i) {\n      if (command[i] == 'G') ans += \"G\";\n      else if (command[i] == '(') {\n        if (command[i + 1] == ')') ans += \"o\";\n        else ans += \"al\";\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def interpret(self, command: str) -> str:\n    ans = []\n    for i, c in enumerate(command):\n      if c == 'G': ans.append('G')\n      elif c == '(':\n        ans.append('o' if command[i + 1] == ')' else 'al')\n    return ''.join(ans)\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You own a&nbsp;Goal Parser&nbsp;that can interpret a string&nbsp;command. The&nbsp;command&nbsp;consists of an alphabet of&nbsp;&#8220;G&#8221;,&nbsp;&#8220;()&#8221;&nbsp;and\/or&nbsp;&#8220;(al)&#8221;&nbsp;in some order. The Goal Parser will interpret&nbsp;&#8220;G&#8221;&nbsp;as the string&nbsp;&#8220;G&#8221;,&nbsp;&#8220;()&#8221;&nbsp;as the string&nbsp;&#8220;o&#8221;, and&nbsp;&#8220;(al)&#8221;&nbsp;as&#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":[222,171,4],"class_list":["post-7765","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-parser","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7765","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=7765"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7765\/revisions"}],"predecessor-version":[{"id":7767,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7765\/revisions\/7767"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}