|
| 1 | +--- |
| 2 | +id: find-duplicate-file-in-system |
| 3 | +title: Find Duplicate File in System |
| 4 | +sidebar_label: 0609 - Find Duplicate File in System |
| 5 | +tags: |
| 6 | + - String |
| 7 | + - Hash Table |
| 8 | + - Array |
| 9 | +description: "This is a solution to the Find Duplicate File in System problem on LeetCode." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +Given a list `paths` of directory info, including the directory path, and all the files with contents in this directory, return all the duplicate files in the file system in terms of their paths. You may return the answer in **any order**. |
| 15 | + |
| 16 | +A group of duplicate files consists of at least two files that have the same content. |
| 17 | + |
| 18 | +A single directory info string in the input list has the following format: |
| 19 | + |
| 20 | +- `"root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)"` |
| 21 | + |
| 22 | +It means there are `n` files `(f1.txt, f2.txt ... fn.txt)` with content `(f1_content, f2_content ... fn_content)` respectively in the directory `"root/d1/d2/.../dm"`. Note that `n >= 1` and `m >= 0`. If `m = 0`, it means the directory is just the root directory. |
| 23 | + |
| 24 | +The output is a list of groups 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: |
| 25 | + |
| 26 | +- `"directory_path/file_name.txt"` |
| 27 | + |
| 28 | +### Examples |
| 29 | + |
| 30 | +**Example 1:** |
| 31 | + |
| 32 | +``` |
| 33 | +Input: paths = ["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)","root 4.txt(efgh)"] |
| 34 | +Output: [["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]] |
| 35 | +``` |
| 36 | + |
| 37 | +**Example 2:** |
| 38 | + |
| 39 | +``` |
| 40 | +Input: paths = ["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)"] |
| 41 | +Output: [["root/a/2.txt","root/c/d/4.txt"],["root/a/1.txt","root/c/3.txt"]] |
| 42 | +``` |
| 43 | + |
| 44 | +### Constraints |
| 45 | + |
| 46 | +- $1 \leq \text{paths.length} \leq 2 \times 10^4$ |
| 47 | +- $1 \leq paths[i].length \leq 3000$ |
| 48 | +- $1 \leq sum(paths[i].length) \leq 5 * 10^5$ |
| 49 | +- `paths[i]` consist of English letters, digits, `'/'`, `'.'`, `'('`, `')'`, and `' '`. |
| 50 | +- You may assume no files or directories share the same name in the same directory. |
| 51 | +- You may assume each given directory info represents a unique directory. A single blank space separates the directory path and file info. |
| 52 | + |
| 53 | +## Solution for Find Duplicate File in System |
| 54 | + |
| 55 | +### Approach 1 Brute Force [Time Limit Exceeded] |
| 56 | + |
| 57 | +For the brute force solution, firstly we obtain the directory paths, the filenames and file contents separately by appropriately splitting the elements of the pathspathspaths list. While doing so, we keep on creating a list which contains the full path of every file along with the contents of the file. The list contains data in the form |
| 58 | + |
| 59 | +> [[file_1_full_path,file_1_contents],[file_2_full_path,file_2_contents]...,[file_n_full_path,file_n_contents]] |
| 60 | +
|
| 61 | +Once this is done, we iterate over this list. For every element i chosen from the list, we iterate over the whole list to find another element j whose file contents are the same as the $i^{th}$ element. For every such element found, we put the $j^{th}$ element's file path in a temporary list l and we also mark the $j^{th}$ 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 $i^{th}$ element, we obtain a list of file paths in l, which have the same contents as the file corresponding to the $i^{th}$ element. If this list isn't empty, it indicates that there exists content duplicate to the $i^{th}$ element. Thus, we also need to put the $i^{th}$ element's file path in the l. |
| 62 | + |
| 63 | +At the end of each iteration, we put this list l obtained in the resultant list res and reset the list l for finding the duplicates of the next element. |
| 64 | + |
| 65 | +## Code in Different Languages |
| 66 | + |
| 67 | +<Tabs> |
| 68 | +<TabItem value="cpp" label="C++"> |
| 69 | + <SolutionAuthor name="@Shreyash3087"/> |
| 70 | + |
| 71 | +```cpp |
| 72 | +#include <vector> |
| 73 | +#include <string> |
| 74 | +#include <sstream> |
| 75 | +#include <unordered_map> |
| 76 | +#include <algorithm> |
| 77 | + |
| 78 | +class Solution { |
| 79 | +public: |
| 80 | + std::vector<std::vector<std::string>> findDuplicate(std::vector<std::string>& paths) { |
| 81 | + std::vector<std::pair<std::string, std::string>> list; |
| 82 | + for (const auto& path : paths) { |
| 83 | + std::istringstream iss(path); |
| 84 | + std::string dir; |
| 85 | + iss >> dir; |
| 86 | + std::string file; |
| 87 | + while (iss >> file) { |
| 88 | + auto pos = file.find('('); |
| 89 | + std::string fileName = file.substr(0, pos); |
| 90 | + std::string content = file.substr(pos + 1, file.size() - pos - 2); |
| 91 | + list.emplace_back(dir + "/" + fileName, content); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + std::vector<bool> visited(list.size(), false); |
| 96 | + std::vector<std::vector<std::string>> res; |
| 97 | + for (size_t i = 0; i < list.size() - 1; ++i) { |
| 98 | + if (visited[i]) continue; |
| 99 | + std::vector<std::string> l; |
| 100 | + for (size_t j = i + 1; j < list.size(); ++j) { |
| 101 | + if (list[i].second == list[j].second) { |
| 102 | + l.push_back(list[j].first); |
| 103 | + visited[j] = true; |
| 104 | + } |
| 105 | + } |
| 106 | + if (!l.empty()) { |
| 107 | + l.push_back(list[i].first); |
| 108 | + res.push_back(l); |
| 109 | + } |
| 110 | + } |
| 111 | + return res; |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +``` |
| 116 | +</TabItem> |
| 117 | +<TabItem value="java" label="Java"> |
| 118 | + <SolutionAuthor name="@Shreyash3087"/> |
| 119 | + |
| 120 | +```java |
| 121 | +public class Solution { |
| 122 | + public List < List < String >> findDuplicate(String[] paths) { |
| 123 | + List < String[] > list = new ArrayList < > (); |
| 124 | + for (String path: paths) { |
| 125 | + String[] values = path.split(" "); |
| 126 | + for (int i = 1; i < values.length; i++) { |
| 127 | + String[] name_cont = values[i].split("\\("); |
| 128 | + name_cont[1] = name_cont[1].replace(")", ""); |
| 129 | + list.add(new String[] { |
| 130 | + values[0] + "/" + name_cont[0], name_cont[1] |
| 131 | + }); |
| 132 | + } |
| 133 | + } |
| 134 | + boolean[] visited = new boolean[list.size()]; |
| 135 | + List < List < String >> res = new ArrayList < > (); |
| 136 | + for (int i = 0; i < list.size() - 1; i++) { |
| 137 | + if (visited[i]) |
| 138 | + continue; |
| 139 | + List < String > l = new ArrayList < > (); |
| 140 | + for (int j = i + 1; j < list.size(); j++) { |
| 141 | + if (list.get(i)[1].equals(list.get(j)[1])) { |
| 142 | + l.add(list.get(j)[0]); |
| 143 | + visited[j] = true; |
| 144 | + } |
| 145 | + } |
| 146 | + if (l.size() > 0) { |
| 147 | + l.add(list.get(i)[0]); |
| 148 | + res.add(l); |
| 149 | + } |
| 150 | + } |
| 151 | + return res; |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +``` |
| 156 | + |
| 157 | +</TabItem> |
| 158 | +<TabItem value="python" label="Python"> |
| 159 | + <SolutionAuthor name="@Shreyash3087"/> |
| 160 | + |
| 161 | +```python |
| 162 | +from collections import defaultdict |
| 163 | + |
| 164 | +class Solution: |
| 165 | + def findDuplicate(self, paths): |
| 166 | + list_ = [] |
| 167 | + for path in paths: |
| 168 | + values = path.split(" ") |
| 169 | + for i in range(1, len(values)): |
| 170 | + name_cont = values[i].split("(") |
| 171 | + name_cont[1] = name_cont[1].replace(")", "") |
| 172 | + list_.append([values[0] + "/" + name_cont[0], name_cont[1]]) |
| 173 | + |
| 174 | + visited = [False] * len(list_) |
| 175 | + res = [] |
| 176 | + for i in range(len(list_) - 1): |
| 177 | + if visited[i]: |
| 178 | + continue |
| 179 | + l = [] |
| 180 | + for j in range(i + 1, len(list_)): |
| 181 | + if list_[i][1] == list_[j][1]: |
| 182 | + l.append(list_[j][0]) |
| 183 | + visited[j] = True |
| 184 | + if l: |
| 185 | + l.append(list_[i][0]) |
| 186 | + res.append(l) |
| 187 | + |
| 188 | + return res |
| 189 | +``` |
| 190 | +</TabItem> |
| 191 | +</Tabs> |
| 192 | + |
| 193 | +## Complexity Analysis |
| 194 | + |
| 195 | +### Time Complexity: $O(n \times x + f^2 \times s)$ |
| 196 | + |
| 197 | +> **Reason**: Creation of list will take O(n∗x), where n is the number of directories and x is the average string length. Every file is compared with every other file. Let f files are there with average size of s, then files comparision will take O(f2∗s), equals can take O(s). Here, Worst case will be when all files are unique. |
| 198 | +
|
| 199 | +### Space Complexity: $O(n \times x)$ |
| 200 | + |
| 201 | +> **Reason**: Size of lists res and list can grow upto n∗x. |
| 202 | +
|
| 203 | +### Approach 2 Using HashMap |
| 204 | +#### Algorithm |
| 205 | + |
| 206 | +In this approach, firstly we obtain the directory paths, the file names and their contents separately by appropriately splitting each string in the given paths list. In order to find the files with duplicate contents, we make use of a HashMap map, which stores the data in the form (contents,list_of_file_paths_with_this_content). 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 map, with the current contents as the key and the value being a list with only one entry(the current file's path). |
| 207 | + |
| 208 | +At the end, we find out the contents corresponding to which atleast two file paths exist. We obtain the resultant list res, which is a list of lists containing these file paths corresponding to the same contents. |
| 209 | + |
| 210 | +## Code in Different Languages |
| 211 | + |
| 212 | +<Tabs> |
| 213 | +<TabItem value="cpp" label="C++"> |
| 214 | + <SolutionAuthor name="@Shreyash3087"/> |
| 215 | + |
| 216 | +```cpp |
| 217 | +#include <iostream> |
| 218 | +#include <vector> |
| 219 | +#include <unordered_map> |
| 220 | +#include <sstream> |
| 221 | +#include <string> |
| 222 | +#include <algorithm> |
| 223 | + |
| 224 | +using namespace std; |
| 225 | + |
| 226 | +class Solution { |
| 227 | +public: |
| 228 | + vector<vector<string>> findDuplicate(vector<string>& paths) { |
| 229 | + unordered_map<string, vector<string>> map; |
| 230 | + for (const string& path : paths) { |
| 231 | + istringstream iss(path); |
| 232 | + vector<string> values((istream_iterator<string>(iss)), istream_iterator<string>()); |
| 233 | + for (size_t i = 1; i < values.size(); ++i) { |
| 234 | + size_t pos = values[i].find('('); |
| 235 | + string name = values[i].substr(0, pos); |
| 236 | + string content = values[i].substr(pos + 1, values[i].size() - pos - 2); |
| 237 | + map[content].push_back(values[0] + "/" + name); |
| 238 | + } |
| 239 | + } |
| 240 | + vector<vector<string>> res; |
| 241 | + for (const auto& pair : map) { |
| 242 | + if (pair.second.size() > 1) { |
| 243 | + res.push_back(pair.second); |
| 244 | + } |
| 245 | + } |
| 246 | + return res; |
| 247 | + } |
| 248 | +}; |
| 249 | + |
| 250 | +``` |
| 251 | +</TabItem> |
| 252 | +<TabItem value="java" label="Java"> |
| 253 | + <SolutionAuthor name="@Shreyash3087"/> |
| 254 | +
|
| 255 | +```java |
| 256 | +
|
| 257 | +public class Solution { |
| 258 | + public List < List < String >> findDuplicate(String[] paths) { |
| 259 | + HashMap < String, List < String >> map = new HashMap < > (); |
| 260 | + for (String path: paths) { |
| 261 | + String[] values = path.split(" "); |
| 262 | + for (int i = 1; i < values.length; i++) { |
| 263 | + String[] name_cont = values[i].split("\\("); |
| 264 | + name_cont[1] = name_cont[1].replace(")", ""); |
| 265 | + List < String > list = map.getOrDefault(name_cont[1], new ArrayList < String > ()); |
| 266 | + list.add(values[0] + "/" + name_cont[0]); |
| 267 | + map.put(name_cont[1], list); |
| 268 | + } |
| 269 | + } |
| 270 | + List < List < String >> res = new ArrayList < > (); |
| 271 | + for (String key: map.keySet()) { |
| 272 | + if (map.get(key).size() > 1) |
| 273 | + res.add(map.get(key)); |
| 274 | + } |
| 275 | + return res; |
| 276 | + } |
| 277 | +} |
| 278 | +
|
| 279 | +``` |
| 280 | + |
| 281 | +</TabItem> |
| 282 | +<TabItem value="python" label="Python"> |
| 283 | + <SolutionAuthor name="@Shreyash3087"/> |
| 284 | + |
| 285 | +```python |
| 286 | +from collections import defaultdict |
| 287 | + |
| 288 | +class Solution: |
| 289 | + def findDuplicate(self, paths): |
| 290 | + file_map = defaultdict(list) |
| 291 | + |
| 292 | + for path in paths: |
| 293 | + values = path.split(" ") |
| 294 | + for i in range(1, len(values)): |
| 295 | + name_cont = values[i].split("(") |
| 296 | + name_cont[1] = name_cont[1].replace(")", "") |
| 297 | + file_map[name_cont[1]].append(values[0] + "/" + name_cont[0]) |
| 298 | + |
| 299 | + res = [] |
| 300 | + for key, value in file_map.items(): |
| 301 | + if len(value) > 1: |
| 302 | + res.append(value) |
| 303 | + |
| 304 | + return res |
| 305 | + |
| 306 | +``` |
| 307 | +</TabItem> |
| 308 | +</Tabs> |
| 309 | + |
| 310 | +## Complexity Analysis |
| 311 | + |
| 312 | +### Time Complexity: $O(n \times x)$ |
| 313 | + |
| 314 | +> **Reason**: n strings of average length x is parsed. |
| 315 | + |
| 316 | +### Space Complexity: $O(n \times x)$ |
| 317 | + |
| 318 | +> **Reason**: map and res size grows upto n∗x. |
| 319 | + |
| 320 | +## References |
| 321 | + |
| 322 | +- **LeetCode Problem**: [Find Duplicate File in System](https://leetcode.com/problems/find-duplicate-file-in-system/description/) |
| 323 | + |
| 324 | +- **Solution Link**: [Find Duplicate File in System](https://leetcode.com/problems/find-duplicate-file-in-system/solutions/) |
0 commit comments