<?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>transform Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/transform/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/transform/</link>
	<description></description>
	<lastBuildDate>Mon, 14 Sep 2020 03:04:54 +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>transform Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/transform/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1585. Check If String Is Transformable With Substring Sort Operations</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1585-check-if-string-is-transformable-with-substring-sort-operations/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1585-check-if-string-is-transformable-with-substring-sort-operations/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 13 Sep 2020 08:39:31 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[deque]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[transform]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7374</guid>

					<description><![CDATA[<p>Given two strings&#160;s&#160;and&#160;t, you want to transform string&#160;s&#160;into string&#160;t&#160;using the following&#160;operation any number of times: Choose a&#160;non-empty&#160;substring in&#160;s&#160;and sort it in-place&#160;so the characters are in&#160;ascending&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1585-check-if-string-is-transformable-with-substring-sort-operations/">花花酱 LeetCode 1585. Check If String Is Transformable With Substring Sort Operations</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1585. Check If String Is Transformable With Substring Sort Operations - 刷题找工作 EP356" width="500" height="281" src="https://www.youtube.com/embed/Pkd3FampKBk?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given two strings&nbsp;<code>s</code>&nbsp;and&nbsp;<code>t</code>, you want to transform string&nbsp;<code>s</code>&nbsp;into string&nbsp;<code>t</code>&nbsp;using the following&nbsp;operation any number of times:</p>



<ul><li>Choose a&nbsp;<strong>non-empty</strong>&nbsp;substring in&nbsp;<code>s</code>&nbsp;and sort it in-place&nbsp;so the characters are in&nbsp;<strong>ascending order</strong>.</li></ul>



<p>For example, applying the operation on the underlined substring in&nbsp;<code>"14234"</code>&nbsp;results in&nbsp;<code>"12344"</code>.</p>



<p>Return&nbsp;<code>true</code>&nbsp;if&nbsp;<em>it is possible to transform string&nbsp;<code>s</code>&nbsp;into string&nbsp;<code>t</code></em>. Otherwise,&nbsp;return&nbsp;<code>false</code>.</p>



<p>A&nbsp;<strong>substring</strong>&nbsp;is a contiguous sequence of characters within a string.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "84532", t = "34852"
<strong>Output:</strong> true
<strong>Explanation:</strong> You can transform s into t using the following sort operations:
"84532" (from index 2 to 3) -&gt; "84352"
"84352" (from index 0 to 2) -&gt; "34852"
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "34521", t = "23415"
<strong>Output:</strong> true
<strong>Explanation:</strong> You can transform s into t using the following sort operations:
"34521" -&gt; "23451"
"23451" -&gt; "23415"
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "12345", t = "12435"
<strong>Output:</strong> false
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "1", t = "2"
<strong>Output:</strong> false
</pre>



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



<ul><li><code>s.length == t.length</code></li><li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li><li><code>s</code>&nbsp;and&nbsp;<code>t</code>&nbsp;only contain digits from&nbsp;<code>'0'</code>&nbsp;to&nbsp;<code>'9'</code>.</li></ul>



<p><strong>Solution: Queue</strong></p>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/09/1585-ep356-1.png" alt="" class="wp-image-7379" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/09/1585-ep356-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/09/1585-ep356-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/09/1585-ep356-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/09/1585-ep356-2.png" alt="" class="wp-image-7380" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/09/1585-ep356-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/09/1585-ep356-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/09/1585-ep356-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>We can move a smaller digit from right to left by sorting two adjacent digits. <br>e.g. 1857<strong>2</strong> -&gt; 185<strong>2</strong>7 -&gt; 18<strong>2</strong>57 -&gt; 1<strong>2</strong>857, but we can not move a larger to the left of a smaller one.</p>



<p>Thus, for each digit in the target string, we find the first occurrence of it in s, and try to move it to the front by checking if there is any smaller one in front of it.</p>



<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">class Solution {
public:
  bool isTransformable(string s, string t) {
    vector&lt;deque&lt;int&gt;&gt; idx(10);
    for (int i = 0; i &lt; s.length(); ++i)
      idx[s[i] - '0'].push_back(i);
    for (char c : t) {
      const int d  = c - '0';
      if (idx[d].empty()) return false;
      for (int i = 0; i &lt; d; ++i)
        if (!idx[i].empty() &amp;&amp; idx[i].front() &lt; idx[d].front())
          return false;
      idx[d].pop_front();      
    }
    return true;
  }
};</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution:
  def isTransformable(self, s: str, t: str) -&gt; bool:
    idx = defaultdict(deque)
    for i, c in enumerate(s):
      idx[int(c)].append(i)
    for c in t:
      d = int(c)
      if not idx[d]: return False
      for i in range(d):
        if idx[i] and idx[i][0] &lt; idx[d][0]: return False
      idx[d].popleft()
    return True</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1585-check-if-string-is-transformable-with-substring-sort-operations/">花花酱 LeetCode 1585. Check If String Is Transformable With Substring Sort Operations</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-1585-check-if-string-is-transformable-with-substring-sort-operations/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
