{"id":5272,"date":"2019-06-30T23:32:46","date_gmt":"2019-07-01T06:32:46","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5272"},"modified":"2019-07-20T22:18:31","modified_gmt":"2019-07-21T05:18:31","slug":"leetcode-1106-parsing-a-boolean-expression","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/recursion\/leetcode-1106-parsing-a-boolean-expression\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1106. Parsing A Boolean Expression"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1106. Parsing A Boolean Expression - \u5237\u9898\u627e\u5de5\u4f5c EP254\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/y2kFBqj_J08?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>\n<\/div><\/figure>\n\n\n\n<p>Return the result of evaluating a given boolean&nbsp;<code>expression<\/code>, represented as a string.<\/p>\n\n\n\n<p>An expression can either be:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>\"t\"<\/code>, evaluating to&nbsp;<code>True<\/code>;<\/li><li><code>\"f\"<\/code>, evaluating to&nbsp;<code>False<\/code>;<\/li><li><code>\"!(expr)\"<\/code>, evaluating to the logical NOT of the inner expression&nbsp;<code>expr<\/code>;<\/li><li><code>\"&amp;(expr1,expr2,...)\"<\/code>, evaluating to the logical AND of 2 or more inner expressions&nbsp;<code>expr1, expr2, ...<\/code>;<\/li><li><code>\"|(expr1,expr2,...)\"<\/code>, evaluating to the logical OR of 2 or more inner expressions&nbsp;<code>expr1, expr2, ...<\/code><\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> expression = \"!(f)\"\n<strong>Output:<\/strong> true\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> expression = \"|(f,t)\"\n<strong>Output:<\/strong> true\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> expression = \"&amp;(t,f)\"\n<strong>Output:<\/strong> false\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> expression = \"|(&amp;(t,f,t),!(t))\"\n<strong>Output:<\/strong> false<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool parseBoolExpr(string expression) {\n    int s = 0;\n    return parse(expression, s);\n  }\nprivate:  \n  bool parse(const string&amp; exp, int&amp; s) {\n    char ch = exp[s++];    \n    if (ch == 't') return true;      \n    if (ch == 'f') return false;\n    if (ch == '!') {\n      bool ans = !parse(exp, ++s);\n      ++s;\n      return ans;\n    } \n    bool is_and = (ch == '&amp;');\n    bool ans = is_and;\n    ++s;\n    while (true) {\n      if (is_and) ans &amp;= parse(exp, s);\n      else ans |= parse(exp, s);\n      if (exp[s++] == ')') break;\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\nclass Solution {\n  private int s;\n  private char[] exp;\n  public boolean parseBoolExpr(String expression) {\n    exp = expression.toCharArray();\n    s = 0;\n    return parse() == 1;\n  }\n      \n  private int parse() {\n    char ch = exp[s++];\n    if (ch == 't') return 1;\n    if (ch == 'f') return 0;\n    if (ch == '!') {\n      ++s;\n      int ans = 1 - parse();\n      ++s;\n      return ans;\n    }\n    boolean is_and = (ch == '&');\n    int ans = is_and ? 1 : 0;\n    ++s;\n    while (true) {\n      if (is_and) ans &= parse();\n      else ans |= parse();\n      if (exp[s++] == ')') break;\n    }\n    return ans;\n  }\n}\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Return the result of evaluating a given boolean&nbsp;expression, represented as a string. An expression can either be: &#8220;t&#8221;, evaluating to&nbsp;True; &#8220;f&#8221;, evaluating to&nbsp;False; &#8220;!(expr)&#8221;, evaluating&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[217,171,17],"class_list":["post-5272","post","type-post","status-publish","format-standard","hentry","category-recursion","tag-hard","tag-parser","tag-recursion","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5272","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=5272"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5272\/revisions"}],"predecessor-version":[{"id":5321,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5272\/revisions\/5321"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}