{"id":1565,"date":"2018-01-08T16:31:28","date_gmt":"2018-01-09T00:31:28","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1565"},"modified":"2018-04-17T09:20:22","modified_gmt":"2018-04-17T16:20:22","slug":"leetcode-758-bold-words-in-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-758-bold-words-in-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 758. Bold Words in String"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 758. Bold Words in String  - \u5237\u9898\u627e\u5de5\u4f5c EP155\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/rUNE3VVcXAs?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<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32\u548c\u4e00\u4e9b\u8981\u52a0\u7c97\u7684\u5355\u8bcd\uff0c\u8fd4\u56de\u52a0\u7c97\u540e\u7684HTML\u4ee3\u7801\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given a set of keywords\u00a0<code>words<\/code>\u00a0and a string\u00a0<code>S<\/code>, make all appearances of all keywords in\u00a0<code>S<\/code>\u00a0bold. Any letters between\u00a0<code>&lt;b&gt;<\/code>\u00a0and\u00a0<code>&lt;\/b&gt;<\/code>\u00a0tags become bold.<\/p>\n<p>The returned string should use the least number of tags possible, and of course the tags should form a valid combination.<\/p>\n<p>For example, given that\u00a0<code>words = [\"ab\", \"bc\"]<\/code>\u00a0and\u00a0<code>S = \"aabcd\"<\/code>, we should return\u00a0<code>\"a&lt;b&gt;abc&lt;\/b&gt;d\"<\/code>. Note that returning\u00a0<code>\"a&lt;b&gt;a&lt;b&gt;b&lt;\/b&gt;c&lt;\/b&gt;d\"<\/code>\u00a0would use more tags, so it is incorrect.<\/p>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li><code>words<\/code>\u00a0has length in range\u00a0<code>[0, 50]<\/code>.<\/li>\n<li><code>words[i]<\/code>\u00a0has length in range\u00a0<code>[1, 10]<\/code>.<\/li>\n<li><code>S<\/code>\u00a0has length in range\u00a0<code>[0, 500]<\/code>.<\/li>\n<li>All characters in\u00a0<code>words[i]<\/code>\u00a0and\u00a0<code>S<\/code>\u00a0are lowercase letters.<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2520\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/758-ep155.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/758-ep155.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/758-ep155-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/758-ep155-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<p>Time complexity: O(nL^2)<\/p>\n<p>Space complexity: O(n + d)<\/p>\n<p>d: size of dictionary<\/p>\n<p>L: max length of the word which is 10.<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 13 ms\r\nclass Solution {\r\npublic:\r\n    string boldWords(vector&lt;string&gt;&amp; words, string S) {\r\n      const int kMaxWordLen = 10;      \r\n      unordered_set&lt;string&gt; dict(words.begin(), words.end());\r\n      \r\n      int n = S.length();\r\n      vector&lt;int&gt; bolded(n, 0);\r\n      for (int i = 0; i &lt; n; ++i)\r\n        for (int l = 1; l &lt;= min(n - i, kMaxWordLen); ++l)\r\n          if (dict.count(S.substr(i, l)))\r\n            std::fill(bolded.begin() + i, bolded.begin() + i + l, 1);\r\n      \r\n      string ans;\r\n      for (int i = 0; i &lt; n; ++i) {\r\n        if (bolded[i] &amp;&amp; (i == 0 || !bolded[i - 1])) ans += \"&lt;b&gt;\";\r\n        ans += S[i];\r\n        if (bolded[i] &amp;&amp; (i == n - 1 || !bolded[i + 1])) ans += \"&lt;\/b&gt;\";\r\n      }\r\n      \r\n      return ans;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32\u548c\u4e00\u4e9b\u8981\u52a0\u7c97\u7684\u5355\u8bcd\uff0c\u8fd4\u56de\u52a0\u7c97\u540e\u7684HTML\u4ee3\u7801\u3002 Problem: Given a set of keywords\u00a0words\u00a0and a string\u00a0S, make all appearances of all keywords in\u00a0S\u00a0bold. Any letters between\u00a0&lt;b&gt;\u00a0and\u00a0&lt;\/b&gt;\u00a0tags become bold. The returned string should&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163,70,47],"tags":[],"class_list":["post-1565","post","type-post","status-publish","format-standard","hentry","category-easy","category-hashtable","category-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1565","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=1565"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1565\/revisions"}],"predecessor-version":[{"id":2521,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1565\/revisions\/2521"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}