Skip to content

Commit eab0e47

Browse files
authored
Create 1233_Remove_Sub-Folders_from_the_Filesystem.md
1 parent 01f03ba commit eab0e47

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## 1233. Remove Sub-Folders from the Filesystem
2+
3+
4+
```
5+
Given a list of folders, remove all sub-folders in those folders and return in any order the folders after removing.
6+
7+
If a folder[i] is located within another folder[j], it is called a sub-folder of it.
8+
9+
The format of a path is one or more concatenated strings of the form: / followed by one or more lowercase English letters. For example, /leetcode and /leetcode/problems are valid paths while an empty string and / are not.
10+
11+
12+
13+
Example 1:
14+
15+
Input: folder = ["/a","/a/b","/c/d","/c/d/e","/c/f"]
16+
Output: ["/a","/c/d","/c/f"]
17+
Explanation: Folders "/a/b/" is a subfolder of "/a" and "/c/d/e" is inside of folder "/c/d" in our filesystem.
18+
Example 2:
19+
20+
Input: folder = ["/a","/a/b/c","/a/b/d"]
21+
Output: ["/a"]
22+
Explanation: Folders "/a/b/c" and "/a/b/d/" will be removed because they are subfolders of "/a".
23+
Example 3:
24+
25+
Input: folder = ["/a/b/c","/a/b/ca","/a/b/d"]
26+
Output: ["/a/b/c","/a/b/ca","/a/b/d"]
27+
28+
29+
Constraints:
30+
31+
1 <= folder.length <= 4 * 10^4
32+
2 <= folder[i].length <= 100
33+
folder[i] contains only lowercase letters and '/'
34+
folder[i] always starts with character '/'
35+
Each folder name is unique.
36+
```
37+
38+
```python
39+
class Solution:
40+
def removeSubfolders(self, folder: List[str]) -> List[str]:
41+
# sort folder
42+
sorted_folder = sorted(folder)
43+
folder_set = set()
44+
45+
for f in sorted_folder:
46+
# n = len(f)
47+
exist = False
48+
for i,c in enumerate(f):
49+
if c == '/':
50+
if f[:i] in folder_set:
51+
exist = True
52+
break
53+
54+
if not exist:
55+
folder_set.add(f)
56+
57+
return folder_set
58+
```
59+
60+
61+
```
62+
Runtime: 408 ms, faster than 28.41% of Python3 online submissions for Remove Sub-Folders from the Filesystem.
63+
Memory Usage: 30.3 MB, less than 34.29% of Python3 online submissions for Remove Sub-Folders from the Filesystem.
64+
```

0 commit comments

Comments
 (0)