<?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>file Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/file/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/file/</link>
	<description></description>
	<lastBuildDate>Wed, 21 Mar 2018 04:24: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>file Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/file/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 609. Find Duplicate File in System</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-609-find-duplicate-file-in-system/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-609-find-duplicate-file-in-system/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 21 Mar 2018 04:21:35 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[path]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2258</guid>

					<description><![CDATA[<p>Problem https://leetcode.com/problems/find-duplicate-file-in-system/description/ 题目大意：输出系统中文件内容相同的文件名。 Given a list of directory info including directory path, and all the files with contents in this directory, you need to find&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-609-find-duplicate-file-in-system/">花花酱 LeetCode 609. Find Duplicate File in System</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/find-duplicate-file-in-system/description/">https://leetcode.com/problems/find-duplicate-file-in-system/description/</a></p>
<p>题目大意：输出系统中文件内容相同的文件名。</p>
<p>Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in terms of their paths.</p>
<p>A group of duplicate files consists of at least <b>two</b> files that have exactly the same content.</p>
<p>A single directory info string in the <b>input</b> list has the following format:</p>
<p><code>"root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)"</code></p>
<p>It means there are <b>n</b> files (<code>f1.txt</code>, <code>f2.txt</code> &#8230; <code>fn.txt</code> with content <code>f1_content</code>, <code>f2_content</code> &#8230; <code>fn_content</code>, respectively) in directory <code>root/d1/d2/.../dm</code>. Note that n &gt;= 1 and m &gt;= 0. If m = 0, it means the directory is just the root directory.</p>
<p>The <b>output</b> is a list of group of duplicate file paths. For each group, it contains all the file paths of the files that have the same content. A file path is a string that has the following format:</p>
<p><code>"directory_path/file_name.txt"</code></p>
<p><b>Example 1:</b></p>
<pre class="crayon:false "><b>Input:</b>
["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"]
<b>Output:</b>  
[["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]]
</pre>
<p><b>Note:</b></p>
<ol>
<li>No order is required for the final output.</li>
<li>You may assume the directory name, file name and file content only has letters and digits, and the length of file content is in the range of [1,50].</li>
<li>The number of files given is in the range of [1,20000].</li>
<li>You may assume no files or directories share the same name in the same directory.</li>
<li>You may assume each given directory info represents a unique directory. Directory path and file info are separated by a single blank space.</li>
</ol>
<h1><b>Follow-up beyond contest:</b></h1>
<ol>
<li>Imagine you are given a real file system, how will you search files? DFS or BFS?</li>
<li>If the file content is very large (GB level), how will you modify your solution?</li>
<li>If you can only read the file by 1kb each time, how will you modify your solution?</li>
<li>What is the time complexity of your modified solution? What is the most time-consuming part and memory consuming part of it? How to optimize?</li>
<li>How to make sure the duplicated files you find are not false positive?</li>
</ol>
<h1><strong>Solution: HashTable</strong></h1>
<p>Key: content, Value: Array of filenames</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 80ms (beats 94.68%)
class Solution {
public:
  vector&lt;vector&lt;string&gt;&gt; findDuplicate(vector&lt;string&gt;&amp; paths) {
    unordered_map&lt;string, vector&lt;string&gt;&gt; files; // content -&gt; filenames
    for (const string&amp; path : paths) {
      string folder;
      int i = 0;
      while (path[i] != ' ') folder += path[i++]; 
      while (i &lt; path.length()) {
        string filename;
        string content;
        ++i; // ' '
        while (path[i] != '(') filename += path[i++];
        ++i; // '('
        while (path[i] != ')') content += path[i++];
        ++i; // ')'        
        files[content].push_back(folder + "/" + filename);
      }      
    }
    vector&lt;vector&lt;string&gt;&gt; ans;
    for (auto&amp; kv : files) 
      if (kv.second.size() &gt; 1)
        ans.push_back(std::move(kv.second));      
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-609-find-duplicate-file-in-system/">花花酱 LeetCode 609. Find Duplicate File in System</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-609-find-duplicate-file-in-system/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
