Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/acca4e95e01e27b00d6f4306a3b4d67b to your computer and use it in GitHub Desktop.
Save jianminchen/acca4e95e01e27b00d6f4306a3b4d67b to your computer and use it in GitHub Desktop.
Leetcode 609 - find duplicate file in system - pass online judge
public class Solution {
public IList<IList<string>> FindDuplicate(string[] paths) {
var dictionary = new Dictionary<string, List<string>>();
foreach(var path in paths)
{
var words = path.Split(' ');
var root = words[0];
for(int i = 1; i < words.Length; i++)
{
var visit = words[i];
var index = visit.IndexOf('(');
var fileName = visit.Substring(0, index);
var content = visit.Substring(index + 1, visit.Length - index - 1); //
var fileFullInfo = root + "/" + fileName;
if(dictionary.ContainsKey(content))
{
var list = dictionary[content];
list.Add(fileFullInfo);
dictionary[content] = list;
}
else
{
var list = new List<string>();
list.Add(fileFullInfo);
dictionary.Add(content, list);
}
}
}
var result = new List<IList<string>>();
foreach(var pair in dictionary)
{
var list = pair.Value;
if(list.Count <= 1)
{
continue;
}
result.Add(list);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment