<?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>contains Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/contains/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/contains/</link>
	<description></description>
	<lastBuildDate>Wed, 11 Jul 2018 01:54:26 +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>contains Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/contains/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 676. Implement Magic Dictionary</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-676-implement-magic-dictionary/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-676-implement-magic-dictionary/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Sep 2017 21:00:40 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[contains]]></category>
		<category><![CDATA[dict]]></category>
		<category><![CDATA[transform]]></category>
		<category><![CDATA[trie]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=227</guid>

					<description><![CDATA[<p>Problem: Implement a magic directory with buildDict, and search methods. For the method buildDict, you&#8217;ll be given a list of non-repetitive words to build a dictionary. For the method search,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-676-implement-magic-dictionary/">花花酱 LeetCode 676. Implement Magic Dictionary</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><iframe width="500" height="375" src="https://www.youtube.com/embed/wq9XjoKMxek?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<div class="question-description">
<p>Implement a magic directory with <code>buildDict</code>, and <code>search</code> methods.</p>
<p>For the method <code>buildDict</code>, you&#8217;ll be given a list of non-repetitive words to build a dictionary.</p>
<p>For the method <code>search</code>, you&#8217;ll be given a word, and judge whether if you modify <b>exactly</b> one character into <b>another</b> character in this word, the modified word is in the dictionary you just built.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: buildDict([&quot;hello&quot;, &quot;leetcode&quot;]), Output: Null
Input: search(&quot;hello&quot;), Output: False
Input: search(&quot;hhllo&quot;), Output: True
Input: search(&quot;hell&quot;), Output: False
Input: search(&quot;leetcoded&quot;), Output: False</pre><p><b>Note:</b></p>
<ol>
<li>You may assume that all the inputs are consist of lowercase letters <code>a-z</code>.</li>
<li>For contest purpose, the test data is rather small by now. You could think about highly efficient algorithm after the contest.</li>
<li>Please remember to <b>RESET</b> your class variables declared in class MagicDictionary, as static/class variables are <b>persisted across multiple test cases</b>. Please see <a href="https://leetcode.com/faq/#different-output">here</a> for more details.</li>
</ol>
</div>
<div id="interviewed-div"><strong>Idea:</strong></div>
<div></div>
<div>Fuzzy match</div>
<div></div>
<div><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/676-ep49-1.png"><img class="alignnone size-full wp-image-231" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/676-ep49-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/676-ep49-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/676-ep49-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/676-ep49-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/676-ep49-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></div>
<div></div>
<div><strong>Time Complexity:</strong></div>
<div>buildDict: O(n*m)</div>
<div>n: numbers of words</div>
<div>m: length of word</div>
<div></div>
<div>search: O(m)</div>
<div>m: length of word</div>
<div></div>
<div><strong>Space Complexity:</strong></div>
<div>O(n*m)</div>
<div></div>
<div><strong>Solution:</strong></div>
<div>
<pre class="crayon-plain-tag">// Author: Huahua
class MagicDictionary {
public:
    /** Initialize your data structure here. */
    MagicDictionary() {
        dict_.clear();
    }
    
    /** Build a dictionary through a list of words */
    void buildDict(vector&lt;string&gt; dict) {
        for(string&amp; word: dict) {
            for(int i = 0; i &lt; word.length(); ++i) {
                char c = word[i];
                word[i] = '*';
                dict_[word].insert(c);
                word[i] = c;
            }
        }
    }
    
    /** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
    bool search(string word) {
        for(int i = 0; i &lt; word.length(); ++i) {
            char c = word[i];
            word[i] = '*';
            if (dict_.count(word)) {
                const auto&amp; char_set = dict_[word];
                if (!char_set.count(c) || char_set.size() &gt; 1)
                    return true;
            }
            word[i] = c;
        }
        return false;
    }
private:
    unordered_map&lt;string, unordered_set&lt;char&gt;&gt; dict_;
};

/**
 * Your MagicDictionary object will be instantiated and called as such:
 * MagicDictionary obj = new MagicDictionary();
 * obj.buildDict(dict);
 * bool param_2 = obj.search(word);
 */</pre>
</div>
<p>Java / Trie</p><pre class="crayon-plain-tag">class MagicDictionary {
    private Trie root;    
    
    public MagicDictionary() {
        root = new Trie();
    } 
    
    public void buildDict(String[] dict) {
        for (String word : dict) {
            Trie curr = root;
            for (char c : word.toCharArray()) {                
                int index = c - 'a';
                if (curr.children[index] == null)
                    curr.children[index] = new Trie();
                curr = curr.children[index];
            }
            curr.ends = true;
        }
    }  
    
    public boolean search(String word) {
        char[] s = word.toCharArray();
        for(int i = 0; i &lt; s.length; i++) {
            for (char c = 'a'; c &lt;= 'z'; ++c) {
                if (s[i] == c) continue;
                char tmp = s[i];
                s[i] = c;
                if (contains(s)) return true;
                s[i] = tmp;
            }
        }
        return false;
    }
    
    private boolean contains(char[] word) {        
        Trie curr = root;
        for (int i = 0; i &lt; word.length; ++i) {
            curr = curr.children[word[i] - 'a'];
            if (curr == null) return false;            
        }
        return curr.ends;
    }
    
    class Trie {
        private boolean ends;
        private Trie[] children;
        public Trie() {
            children = new Trie[26];
            ends = false;
        }
    }
}</pre><p>Java / Trie v2</p><pre class="crayon-plain-tag">class MagicDictionary {
    private Trie root;    
    
    public MagicDictionary() {
        root = new Trie();
    } 
    
    public void buildDict(String[] dict) {
        for (String word : dict) {
            Trie curr = root;
            for (char c : word.toCharArray()) {                
                int index = c - 'a';
                if (curr.children[index] == null)
                    curr.children[index] = new Trie();
                curr = curr.children[index];
            }
            curr.ends = true;
        }
    }  
    
    public boolean search(String word) {
        char[] s = word.toCharArray();
        Trie prefix = root;
        for(int i = 0; i &lt; s.length; i++) {
            for (char c = 'a'; c &lt;= 'z'; ++c) {
                if (s[i] == c) continue;
                char tmp = s[i];
                s[i] = c;
                if (contains(s, prefix, i)) return true;
                s[i] = tmp;                
            }
            prefix = prefix.children[s[i] - 'a'];
            if (prefix == null) return false;
        }
        return false;
    }
    
    private boolean contains(char[] word, Trie prefix, int s) {        
        Trie curr = prefix;
        for (int i = s; i &lt; word.length; ++i) {
            curr = curr.children[word[i] - 'a'];
            if (curr == null) return false;
        }
        return curr.ends;
    }
    
    class Trie {
        private boolean ends;
        private Trie[] children;
        public Trie() {
            children = new Trie[26];
            ends = false;
        }
    }
}</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-676-implement-magic-dictionary/">花花酱 LeetCode 676. Implement Magic Dictionary</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/hashtable/leetcode-676-implement-magic-dictionary/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
