
Sponsored
Sponsored
To find duplicate files by their content, we can use a HashMap (or Dictionary). For each directory info string, parse the directory path and the files with their contents. Use the content as the key in the map and store full path of the file as its value. After parsing all inputs, the map keys with more than one value represent duplicate files.
Time Complexity: O(n), where n is the total number of characters in all file paths. We iterate over each character once.
Space Complexity: O(n) to store the lists of file paths in the dictionary.
1using System;
2using System.Collections.Generic;
3
4public class DuplicateFiles
5{
6 public IList<IList<string>> FindDuplicate(string[] paths)
7 {
8 var contentMap = new Dictionary<string, List<string>>();
9 foreach (var path in paths)
10 {
11 var parts = path.Split(' ');
12 var root = parts[0];
13 for (int i = 1; i < parts.Length; ++i)
14 {
15 var nameContent = parts[i].Split('(');
16 var name = nameContent[0];
17 var content = nameContent[1].Substring(0, nameContent[1].Length - 1);
18 if (!contentMap.ContainsKey(content))
19 {
20 contentMap[content] = new List<string>();
21 }
22 contentMap[content].Add(root + "/" + name);
23 }
24 }
25 var result = new List<IList<string>>();
26 foreach (var group in contentMap.Values)
27 {
28 if (group.Count > 1)
29 {
30 result.Add(group);
31 }
32 }
33 return result;
34 }
35}In C#, the Dictionary class facilitates mapping file content to its corresponding paths. The split method aids in processing directories and files, allowing us to accurately extract and group paths by content key. The final step involves filtering and returning only those entries which have duplicate file path lists.
This approach is based on directly processing string data and arranging results using a 2D array. Strings are manipulated to extract directory data, file names, and contents into standalone variables, then append paths to a growing structure. Compared to hash maps, this method uses arrays to aggregate identical files.
Time Complexity: O(n), where n is the total input character count due to one-pass evaluation.
Space Complexity: O(n), maintaining paths and intermediate arrays.
1using System;
2using System.Collections.Generic;
public class DuplicateWithArrays
{
public IList<IList<string>> FindDuplicate(string[] paths)
{
Dictionary<string, List<string>> contentMap = new Dictionary<string, List<string>>();
foreach (string path in paths)
{
string[] parts = path.Split(' ');
string directory = parts[0];
for (int i = 1; i < parts.Length; i++)
{
int openParenIndex = parts[i].IndexOf('(');
int closeParenIndex = parts[i].IndexOf(')', openParenIndex);
string name = parts[i].Substring(0, openParenIndex);
string content = parts[i].Substring(openParenIndex + 1, closeParenIndex - openParenIndex - 1);
if (!contentMap.ContainsKey(content))
{
contentMap[content] = new List<string>();
}
contentMap[content].Add(directory + "/" + name);
}
}
var result = new List<IList<string>>();
foreach (var pair in contentMap)
{
if (pair.Value.Count > 1)
{
result.Add(pair.Value);
}
}
return result;
}
}Utilizing C#'s native string capabilities, this implementation deliberately processes content without relying on heavy structuring, using simple substring and index functions to group matching file paths by their contents.