1948. Delete Duplicate Folders in System Hard
1/**
2 * [1948] Delete Duplicate Folders in System
3 *
4 * Due to a bug, there are many duplicate folders in a file system. You are given a 2D array paths, where paths[i] is an array representing an absolute path to the i^th folder in the file system.
5 *
6 * For example, ["one", "two", "three"] represents the path "/one/two/three".
7 *
8 * Two folders (not necessarily on the same level) are identical if they contain the same non-empty set of identical subfolders and underlying subfolder structure. The folders do not need to be at the root level to be identical. If two or more folders are identical, then mark the folders as well as all their subfolders.
9 *
10 * For example, folders "/a" and "/b" in the file structure below are identical. They (as well as their subfolders) should all be marked:
11 *
12 * /a
13 * /a/x
14 * /a/x/y
15 * /a/z
16 * /b
17 * /b/x
18 * /b/x/y
19 * /b/z
20 *
21 *
22 * However, if the file structure also included the path "/b/w", then the folders "/a" and "/b" would not be identical. Note that "/a/x" and "/b/x" would still be considered identical even with the added folder.
23 *
24 * Once all the identical folders and their subfolders have been marked, the file system will delete all of them. The file system only runs the deletion once, so any folders that become identical after the initial deletion are not deleted.
25 * Return the 2D array ans containing the paths of the remaining folders after deleting all the marked folders. The paths may be returned in any order.
26 *
27 * Example 1:
28 * <img alt="" src="https://assets.leetcode.com/uploads/2021/07/19/lc-dupfolder1.jpg" style="width: 200px; height: 218px;" />
29 * Input: paths = [["a"],["c"],["d"],["a","b"],["c","b"],["d","a"]]
30 * Output: [["d"],["d","a"]]
31 * Explanation: The file structure is as shown.
32 * Folders "/a" and "/c" (and their subfolders) are marked for deletion because they both contain an empty
33 * folder named "b".
34 *
35 * Example 2:
36 * <img alt="" src="https://assets.leetcode.com/uploads/2021/07/19/lc-dupfolder2.jpg" style="width: 200px; height: 355px;" />
37 * Input: paths = [["a"],["c"],["a","b"],["c","b"],["a","b","x"],["a","b","x","y"],["w"],["w","y"]]
38 * Output: [["c"],["c","b"],["a"],["a","b"]]
39 * Explanation: The file structure is as shown.
40 * Folders "/a/b/x" and "/w" (and their subfolders) are marked for deletion because they both contain an empty folder named "y".
41 * Note that folders "/a" and "/c" are identical after the deletion, but they are not deleted because they were not marked beforehand.
42 *
43 * Example 3:
44 * <img alt="" src="https://assets.leetcode.com/uploads/2021/07/19/lc-dupfolder3.jpg" style="width: 200px; height: 201px;" />
45 * Input: paths = [["a","b"],["c","d"],["c"],["a"]]
46 * Output: [["c"],["c","d"],["a"],["a","b"]]
47 * Explanation: All folders are unique in the file system.
48 * Note that the returned array can be in a different order as the order does not matter.
49 *
50 *
51 * Constraints:
52 *
53 * 1 <= paths.length <= 2 * 10^4
54 * 1 <= paths[i].length <= 500
55 * 1 <= paths[i][j].length <= 10
56 * 1 <= sum(paths[i][j].length) <= 2 * 10^5
57 * path[i][j] consists of lowercase English letters.
58 * No two paths lead to the same folder.
59 * For any folder not at the root level, its parent folder will also be in the input.
60 *
61 */
62pub struct Solution {}
63
64// problem: https://leetcode.com/problems/delete-duplicate-folders-in-system/
65// discuss: https://leetcode.com/problems/delete-duplicate-folders-in-system/discuss/?currentPage=1&orderBy=most_votes&query=
66
67// submission codes start here
68
69impl Solution {
70 pub fn delete_duplicate_folder(paths: Vec<Vec<String>>) -> Vec<Vec<String>> {
71 vec![]
72 }
73}
74
75// submission codes end
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80
81 #[test]
82 fn test_1948() {
83 }
84}
85
Back
© 2025 bowen.ge All Rights Reserved.