1# a TrieNode class for creating new node2class TrieNode:3def __init__(self):4self.children = {}5self.main = False678# the main class9class Solution(object):10def removeSubfolders(self, folder):11node = TrieNode()12res = []13# sort the list to prevent adding the subfolder to the Trie first14folder.sort()15for dir in folder:16name = dir.split("/")17if self.addTrie(name, node):18res.append(dir)19return res2021# usign the same addTrie template and modify the else part22def addTrie(self, name, node):23trie = node24for c in name:25if c not in trie.children:26trie.children[c] = TrieNode()27# if char is in trie,28else:29# check if it's the last sub folder.30if trie.children[c].main == True:31return False32trie = trie.children[c]33trie.main = True34return True