1
# a TrieNode class for creating new node
2
class TrieNode:
3
def __init__(self):
4
self.children = {}
5
self.main = False
6

7

8
# the main class
9
class Solution(object):
10
def removeSubfolders(self, folder):
11
node = TrieNode()
12
res = []
13
# sort the list to prevent adding the subfolder to the Trie first
14
folder.sort()
15
for dir in folder:
16
name = dir.split("/")
17
if self.addTrie(name, node):
18
res.append(dir)
19
return res
20

21
# usign the same addTrie template and modify the else part
22
def addTrie(self, name, node):
23
trie = node
24
for c in name:
25
if c not in trie.children:
26
trie.children[c] = TrieNode()
27
# if char is in trie,
28
else:
29
# check if it's the last sub folder.
30
if trie.children[c].main == True:
31
return False
32
trie = trie.children[c]
33
trie.main = True
34
return True

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0