<?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>mutation Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/mutation/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/mutation/</link>
	<description></description>
	<lastBuildDate>Sat, 24 Mar 2018 17:44:34 +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>mutation Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/mutation/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 433. Minimum Genetic Mutation</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-433-minimum-genetic-mutation/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-433-minimum-genetic-mutation/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 24 Mar 2018 17:39:20 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[mutation]]></category>
		<category><![CDATA[shortest path]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2340</guid>

					<description><![CDATA[<p>Problem 题目大意：给你一个基因库，问一个基因最少需要变异多少次才能变为另外一个基因。每次变异只能修改一个字符，并且必须在基因库里。 https://leetcode.com/problems/minimum-genetic-mutation/description/ A gene string can be represented by an 8-character long string, with choices from "A", "C", "G", "T". Suppose we need to investigate about a mutation&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-433-minimum-genetic-mutation/">花花酱 LeetCode 433. Minimum Genetic Mutation</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>题目大意：给你一个基因库，问一个基因最少需要变异多少次才能变为另外一个基因。每次变异只能修改一个字符，并且必须在基因库里。</p>
<p><a href="https://leetcode.com/problems/minimum-genetic-mutation/description/">https://leetcode.com/problems/minimum-genetic-mutation/description/</a></p>
<p>A gene string can be represented by an 8-character long string, with choices from <code>"A"</code>, <code>"C"</code>, <code>"G"</code>, <code>"T"</code>.</p>
<p>Suppose we need to investigate about a mutation (mutation from &#8220;start&#8221; to &#8220;end&#8221;), where ONE mutation is defined as ONE single character changed in the gene string.</p>
<p>For example, <code>"AACCGGTT"</code> -&gt; <code>"AACCGGTA"</code> is 1 mutation.</p>
<p>Also, there is a given gene &#8220;bank&#8221;, which records all the valid gene mutations. A gene must be in the bank to make it a valid gene string.</p>
<p>Now, given 3 things &#8211; start, end, bank, your task is to determine what is the minimum number of mutations needed to mutate from &#8220;start&#8221; to &#8220;end&#8221;. If there is no such a mutation, return -1.</p>
<p><b>Note:</b></p>
<ol>
<li>Starting point is assumed to be valid, so it might not be included in the bank.</li>
<li>If multiple mutations are needed, all mutations during in the sequence must be valid.</li>
<li>You may assume start and end string is not the same.</li>
</ol>
<p><b>Example 1:</b></p>
<pre class="crayon:false">start: "AACCGGTT"
end:   "AACCGGTA"
bank: ["AACCGGTA"]

return: 1
</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false">start: "AACCGGTT"
end:   "AAACGGTA"
bank: ["AACCGGTA", "AACCGCTA", "AAACGGTA"]

return: 2
</pre>
<p><b>Example 3:</b></p>
<pre class="crayon:false ">start: "AAAAACCC"
end:   "AACCCCCC"
bank: ["AAAACCCC", "AAACCCCC", "AACCCCCC"]

return: 3</pre>
<h1><strong>Solution: BFS Shortest Path</strong></h1>
<p>Time complexity: O(n^2)</p>
<p>Space complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 4 ms
class Solution {
public:
  int minMutation(string start, string end, vector&lt;string&gt;&amp; bank) {    
    queue&lt;string&gt; q;
    q.push(start);
    
    unordered_set&lt;string&gt; visited;
    visited.insert(start);
    
    int mutations = 0;
    while (!q.empty()) {
      size_t size = q.size();
      while (size--) {
        string curr = std::move(q.front()); q.pop();
        if (curr == end) return mutations;
        for (const string&amp; gene : bank) {
          if (visited.count(gene) || !validMutation(curr, gene)) continue;
          visited.insert(gene);
          q.push(gene);
        }
      }
      ++mutations;
    }    
    return -1;
  }
private:
  bool validMutation(const string&amp; s1, const string&amp; s2) {
    int count = 0;
    for (int i = 0; i &lt; s1.length(); ++i)
      if (s1[i] != s2[i] &amp;&amp; count++) return false;
    return true;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-433-minimum-genetic-mutation/">花花酱 LeetCode 433. Minimum Genetic Mutation</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/graph/leetcode-433-minimum-genetic-mutation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
