Approach #1 Brute Force [Time Limit Exceeded]
For the brute force solution, firstly we obtain the directory paths, the filenames and file contents separately by appropriately splitting the elements of the list. While doing so, we keep on creating a which contains the full path of every file along with the contents of the file. The contains data in the form .
Once this is done, we iterate over this . For every element chosen from the list, we iterate over the whole to find another element whose file contents are the same as the element. For every such element found, we put the element's file path in a temporary list and we also mark the element as visited so that this element isn't considered again in the future. Thus, when we reach the end of the array for every element, we obtain a list of file paths in , which have the same contents as the file corresponding to the element. If this list isn't empty, it indicates that there exists content duplicate to the element. Thus, we also need to put the element's file path in the .
At the end of each iteration, we put this list obtained in the resultant list and reset the list for finding the duplicates of the next element.
Time complexity : . Creation of will take , where n is the number of directories and x is the average string length. Every file is compared with every other file. Let files are there with average size of , then files comparision will take , equals can take . Here, Worst case will be when all files are unique.
Space complexity : . Size of lists and can grow upto .
Approach #2 Using HashMap [Accepted]
In this approach, firstly we obtain the directory paths, the file names and their contents separately by appropriately splitting each string in the given list. In order to find the files with duplicate contents, we make use of a HashMap , which stores the data in the form . Thus, for every file's contents, we check if the same content already exist in the hashmap. If so, we add the current file's path to the list of files corresponding to the current contents. Otherwise, we create a new entry in the , with the current contents as the key and the value being a list with only one entry(the current file's path).
At the end, we find out the contents corresponding to which atleast two file paths exist. We obtain the resultant list , which is a list of lists containing these file paths corresponding to the same contents.
The following animation illustrates the process for a clearer understanding.
Time complexity : . strings of average length is parsed.
Space complexity : . and size grows upto .
Analysis written by: @vinod23