{"id":6600,"date":"2020-04-11T23:35:21","date_gmt":"2020-04-12T06:35:21","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6600"},"modified":"2020-04-11T23:35:50","modified_gmt":"2020-04-12T06:35:50","slug":"leetcode-1410-html-entity-parser","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1410-html-entity-parser\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1410. HTML Entity Parser"},"content":{"rendered":"\n<p><strong>HTML entity parser<\/strong>&nbsp;is the parser that takes HTML code as input and replace all the entities of the special characters by the characters itself.<\/p>\n\n\n\n<p>The special characters and their entities for HTML are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Quotation Mark:<\/strong>&nbsp;the entity is&nbsp;<code>&amp;quot;<\/code>&nbsp;and&nbsp;symbol character is&nbsp;<code>\"<\/code>.<\/li><li><strong>Single Quote&nbsp;Mark:<\/strong>&nbsp;the entity is&nbsp;<code>&amp;apos;<\/code>&nbsp;and&nbsp;symbol character is&nbsp;<code>'<\/code>.<\/li><li><strong>Ampersand:<\/strong>&nbsp;the entity is&nbsp;<code>&amp;amp;<\/code>&nbsp;and symbol character is&nbsp;<code>&amp;<\/code>.<\/li><li><strong>Greater Than Sign:<\/strong>&nbsp;the entity is&nbsp;<code>&amp;gt;<\/code>&nbsp;and symbol character is&nbsp;<code>&gt;<\/code>.<\/li><li><strong>Less Than Sign:<\/strong>&nbsp;the entity is&nbsp;<code>&amp;lt;<\/code>&nbsp;and symbol character is&nbsp;<code>&lt;<\/code>.<\/li><li><strong>Slash:<\/strong>&nbsp;the entity is&nbsp;<code>&amp;frasl;<\/code>&nbsp;and&nbsp;symbol character is&nbsp;<code>\/<\/code>.<\/li><\/ul>\n\n\n\n<p>Given the input&nbsp;<code>text<\/code>&nbsp;string to the HTML parser, you have to implement the entity parser.<\/p>\n\n\n\n<p>Return&nbsp;<em>the text<\/em>&nbsp;after replacing the entities by the special characters.<\/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> text = \"&amp;amp; is an HTML entity but &amp;ambassador; is not.\"\n<strong>Output:<\/strong> \"&amp; is an HTML entity but &amp;ambassador; is not.\"\n<strong>Explanation:<\/strong> The parser will replace the &amp;amp; entity by &amp;\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> text = \"and I quote: &amp;quot;...&amp;quot;\"\n<strong>Output:<\/strong> \"and I quote: \\\"...\\\"\"\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> text = \"Stay home! Practice on Leetcode :)\"\n<strong>Output:<\/strong> \"Stay home! Practice on Leetcode :)\"\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> text = \"x &amp;gt; y &amp;amp;&amp;amp; x &amp;lt; y is always false\"\n<strong>Output:<\/strong> \"x &gt; y &amp;&amp; x &lt; y is always false\"\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> text = \"leetcode.com&amp;frasl;problemset&amp;frasl;all\"\n<strong>Output:<\/strong> \"leetcode.com\/problemset\/all\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= text.length &lt;= 10^5<\/code><\/li><li>The string may contain any possible characters out of all the 256&nbsp;ASCII characters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string entityParser(string text) {    \n    map<string, string> m{\n      {\"&quot;\", \"\\\"\"}, {\"&apos;\", \"'\"}, {\"&amp;\", \"&\"}, \n      {\"&gt;\", \">\"}, {\"&lt;\", \"<\"}, {\"&frasl;\", \"\/\"}};\n    string ans;\n    string buf;\n    for (char c : text) {\n      buf += c;\n      if (buf.back() != ';') continue;\n      const int l = buf.size();\n      for (const auto&#038; [k, v] : m) {\n        const int kl = k.length();\n        if (l >= kl && buf.substr(l - kl) == k) {\n          ans += buf.substr(0, l - kl) + v;\n          buf.clear();\n          break;\n        }            \n      }      \n    }    \n    return ans + buf;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>HTML entity parser&nbsp;is the parser that takes HTML code as input and replace all the entities of the special characters by the characters itself. 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":[48],"tags":[584,177,430,179,4],"class_list":["post-6600","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-html","tag-medium","tag-replace","tag-simulation","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6600","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=6600"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6600\/revisions"}],"predecessor-version":[{"id":6602,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6600\/revisions\/6602"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}