Skip to content

Commit 7d6750b

Browse files
authored
Create remove-sub-folders-from-the-filesystem.py
1 parent 68dc1a2 commit 7d6750b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Time: O(n), n is the total sum of the lengths of folder names
2+
# Space: O(t), t is the number of nodes in trie
3+
4+
import collections
5+
import itertools
6+
7+
8+
class Solution(object):
9+
def removeSubfolders(self, folder):
10+
"""
11+
:type folder: List[str]
12+
:rtype: List[str]
13+
"""
14+
def dfs(curr, path, result):
15+
if "_end" in curr:
16+
result.append("/" + "/".join(path))
17+
return
18+
for c in curr:
19+
if c == "_end":
20+
continue
21+
path.append(c)
22+
dfs(curr[c], path, result)
23+
path.pop()
24+
25+
_trie = lambda: collections.defaultdict(_trie)
26+
trie = _trie()
27+
for f in folder:
28+
f_list = f.split("/")
29+
reduce(dict.__getitem__,
30+
itertools.islice(f_list, 1, len(f_list)),
31+
trie).setdefault("_end")
32+
result = []
33+
dfs(trie, [], result)
34+
return result
35+

0 commit comments

Comments
 (0)