<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>replace Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/replace/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/replace/</link>
	<description></description>
	<lastBuildDate>Sun, 12 Apr 2020 06:35:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>replace Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/replace/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1410. HTML Entity Parser</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-1410-html-entity-parser/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-1410-html-entity-parser/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 12 Apr 2020 06:35:21 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[simulation]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6600</guid>

					<description><![CDATA[<p>HTML entity parser&#160;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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1410-html-entity-parser/">花花酱 LeetCode 1410. HTML Entity Parser</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<p>The special characters and their entities for HTML are:</p>



<ul><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>



<p>Given the input&nbsp;<code>text</code>&nbsp;string to the HTML parser, you have to implement the entity parser.</p>



<p>Return&nbsp;<em>the text</em>&nbsp;after replacing the entities by the special characters.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> text = "&amp;amp; is an HTML entity but &amp;ambassador; is not."
<strong>Output:</strong> "&amp; is an HTML entity but &amp;ambassador; is not."
<strong>Explanation:</strong> The parser will replace the &amp;amp; entity by &amp;
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> text = "and I quote: &amp;quot;...&amp;quot;"
<strong>Output:</strong> "and I quote: \"...\""
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> text = "Stay home! Practice on Leetcode :)"
<strong>Output:</strong> "Stay home! Practice on Leetcode :)"
</pre>



<p><strong>Example 4:</strong></p>



<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"
<strong>Output:</strong> "x &gt; y &amp;&amp; x &lt; y is always false"
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> text = "leetcode.com&amp;frasl;problemset&amp;frasl;all"
<strong>Output:</strong> "leetcode.com/problemset/all"
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: Simulation</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string entityParser(string text) {    
    map&lt;string, string&gt; m{
      {&quot;&amp;quot;&quot;, &quot;\&quot;&quot;}, {&quot;&amp;apos;&quot;, &quot;'&quot;}, {&quot;&amp;amp;&quot;, &quot;&amp;&quot;}, 
      {&quot;&amp;gt;&quot;, &quot;&gt;&quot;}, {&quot;&amp;lt;&quot;, &quot;&lt;&quot;}, {&quot;&amp;frasl;&quot;, &quot;/&quot;}};
    string ans;
    string buf;
    for (char c : text) {
      buf += c;
      if (buf.back() != ';') continue;
      const int l = buf.size();
      for (const auto&amp; [k, v] : m) {
        const int kl = k.length();
        if (l &gt;= kl &amp;&amp; buf.substr(l - kl) == k) {
          ans += buf.substr(0, l - kl) + v;
          buf.clear();
          break;
        }            
      }      
    }    
    return ans + buf;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-1410-html-entity-parser/">花花酱 LeetCode 1410. HTML Entity Parser</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/simulation/leetcode-1410-html-entity-parser/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 648. Replace Words</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-648-replace-words/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-648-replace-words/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 09 Nov 2018 04:42:30 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[prefix]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[trie]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4267</guid>

					<description><![CDATA[<p>Problem https://leetcode.com/problems/replace-words/description/ In English, we have a concept called root, which can be followed by some other words to form another longer word &#8211; let&#8217;s call&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-648-replace-words/">花花酱 LeetCode 648. Replace Words</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p><a href="https://leetcode.com/problems/replace-words/description/">https://leetcode.com/problems/replace-words/description/</a></p>
<p>In English, we have a concept called <code>root</code>, which can be followed by some other words to form another longer word &#8211; let&#8217;s call this word <code>successor</code>. For example, the root <code>an</code>, followed by <code>other</code>, which can form another word <code>another</code>.</p>
<p>Now, given a dictionary consisting of many roots and a sentence. You need to replace all the <code>successor</code> in the sentence with the <code>root</code> forming it. If a <code>successor</code> has many <code>roots</code> can form it, replace it with the root with the shortest length.</p>
<p>You need to output the sentence after the replacement.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b> dict = ["cat", "bat", "rat"]
sentence = "the cattle was rattled by the battery"
<b>Output:</b> "the cat was rat by the bat"
</pre>
<p><b>Note:</b></p>
<ol>
<li>The input will only have lower-case letters.</li>
<li>1 &lt;= dict words number &lt;= 1000</li>
<li>1 &lt;= sentence words number &lt;= 1000</li>
<li>1 &lt;= root length &lt;= 100</li>
<li>1 &lt;= sentence words length &lt;= 1000</li>
</ol>
<h1><strong>Solution 1: HashTable</strong></h1>
<p>Time complexity: O(sum(w^2))</p>
<p>Space complexity: O(sum(l))</p><pre class="crayon-plain-tag">// Author: Huahua, Running time: 84 ms
class Solution {
public:
  string replaceWords(vector&lt;string&gt;&amp; dict, string sentence) {
    unordered_set&lt;string&gt; d(begin(dict), end(dict));
    sentence.push_back(' ');
    string output;
    string word;
    bool found = false;
    for (char c : sentence) {
      if (c == ' ') {
        if (!output.empty()) output += ' ';
        output += word;
        word = "";
        found = false;
        continue;
      }
      if (found) continue;
      word += c;
      if (d.count(word)) {
        found = true;
      }
    }
    return output;
  }
};</pre><p></p>
<h2><strong>Solution2: Trie</strong></h2>
<p>Time complexity: O(sum(l) + n)</p>
<p>Space complexity: O(sum(l) * 26)</p><pre class="crayon-plain-tag">// Author: Huahua, running time: 48 ms
class TrieNode {
public:
  TrieNode(): children(26), is_word(false) {}
  ~TrieNode() {
    for (int i = 0; i &lt; 26; ++i)
      if (children[i]) {
        delete children[i];
        children[i] = nullptr;
      }
  }
  vector&lt;TrieNode*&gt; children;
  bool is_word;
};
class Solution {
public:
  string replaceWords(vector&lt;string&gt;&amp; dict, string sentence) {    
    TrieNode root;
    for (const string&amp; word : dict) {
      TrieNode* cur = &amp;root;
      for (char c : word) {        
        if (!cur-&gt;children[c - 'a']) cur-&gt;children[c - 'a'] = new TrieNode();
        cur = cur-&gt;children[c - 'a'];
      }
      cur-&gt;is_word = true;
    }
    
    string ans;
    stringstream ss(sentence);
    string word;
    while (ss &gt;&gt; word) {      
      TrieNode* cur = &amp;root;
      bool found = false;
      int len = 0;
      for (char c : word) {        
        cur = cur-&gt;children[c - 'a'];        
        if (!cur) break;
        ++len;
        if (cur-&gt;is_word) {
          found = true;
          break;
        }
      }      
      if (!ans.empty()) ans += ' ';      
      ans += found ? word.substr(0, len) : word;
    }
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-648-replace-words/">花花酱 LeetCode 648. Replace Words</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-648-replace-words/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
