You are given a list of folders folder, return the folders after removing all sub-folders in those folders. You may return the answer in any order.
If a folder[i] is located within another folder[j], it is called a sub-folder of it. A sub-folder of folder[j] must start with folder[j], followed by a "/". For example, "/a/b" is a sub-folder of "/a", but "/b" is not a sub-folder of "/a/b".
The format of a path is one or more concatenated strings of the form: '/' followed by one or more lowercase English letters.
For example, "/neetcode" and "/neetcode/practice" are valid paths while an empty string "" and "/" are not.
Example 1:
Input: folder = ["/a","/a/b","/c/d","/c/d/e","/c/f"]
Output: ["/a","/c/d","/c/f"]Explanation: Folders "/a/b" is a subfolder of "/a" and "/c/d/e" is inside of folder "/c/d" in our filesystem.
Example 2:
Input: folder = ["/a","/a/b/c","/a/b/d"]
Output: ["/a"]Explanation: Folders "/a/b/c" and "/a/b/d" will be removed because they are subfolders of "/a".
Example 3:
Input: folder = ["/a/b/c","/a/b/ca","/a/b/d"]
Output: ["/a/b/c","/a/b/ca","/a/b/d"]Constraints:
1 <= folder.length <= 40,0002 <= folder[i].length <= 100folder[i] contains only lowercase letters and '/'.folder[i] always starts with the character '/'.class Solution:
def removeSubfolders(self, folder: List[str]) -> List[str]:
res = []
folder_set = set(folder)
for f in folder:
res.append(f)
for i in range(len(f)):
if f[i] == "/" and f[:i] in folder_set:
res.pop()
break
return resWhere is the size of the string array and is the length of each string.
class Solution:
def removeSubfolders(self, folder: List[str]) -> List[str]:
folder.sort()
res = [folder[0]]
for i in range(1, len(folder)):
if not folder[i].startswith(res[-1] + "/"):
res.append(folder[i])
return resWhere is the size of the string array and is the length of each string.
class Trie:
def __init__(self):
self.children = {} # string -> Trie
self.end_of_folder = False
def add(self, path: str) -> None:
cur = self
for f in path.split("/"):
if f not in cur.children:
cur.children[f] = Trie()
cur = cur.children[f]
cur.end_of_folder = True
def prefix_search(self, path: str) -> bool:
cur = self
folders = path.split("/")
for i in range(len(folders) - 1):
cur = cur.children[folders[i]]
if cur.end_of_folder:
return True
return False
class Solution:
def removeSubfolders(self, folder: List[str]) -> List[str]:
trie = Trie()
for f in folder:
trie.add(f)
res = []
for f in folder:
if not trie.prefix_search(f):
res.append(f)
return resWhere is the size of the string array and is the length of each string.