Created
June 4, 2017 03:31
-
-
Save jianminchen/acca4e95e01e27b00d6f4306a3b4d67b to your computer and use it in GitHub Desktop.
Leetcode 609 - find duplicate file in system - pass online judge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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