diff options
author | Jack Sangdahl <[email protected]> | 2024-10-24 21:55:18 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-10-24 21:55:18 -0600 |
commit | 54e0305c7a0927ea74163843e547dcb93d84344c (patch) | |
tree | 7c2990bf39895c1254d1cda6c67318f16df54f4b /remove_subfolder_filesystem.py | |
parent | 0297643f9bc94881eb3ea3b3f91741fcf34a4d7d (diff) | |
download | leetcode-54e0305c7a0927ea74163843e547dcb93d84344c.tar.gz leetcode-54e0305c7a0927ea74163843e547dcb93d84344c.zip |
1233. remove sub-folders from the filesystem
Diffstat (limited to 'remove_subfolder_filesystem.py')
-rw-r--r-- | remove_subfolder_filesystem.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/remove_subfolder_filesystem.py b/remove_subfolder_filesystem.py new file mode 100644 index 0000000..484831f --- /dev/null +++ b/remove_subfolder_filesystem.py @@ -0,0 +1,34 @@ +# 1233. Remove Sub-Folders from the Filesystem + +""" +given a list of folders 'folder', return the folders after removing all the +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 +'/'. the format of a path is one ormore concatenated strings of the form: '/' +followed by one or more lowercase english letters. +""" + + +class Solution(object): + def removeSubfolders(self, folder): + """ + :type folder: List[str] + :rtype: List[str] + """ + folder.sort(key=lambda x: len(x)) + vis = set() + for i in folder: + for j in range(2, len(i)): + if i[j] == "/" and i[:j] in vis: + break + else: + vis.add(i) + return list(vis) + + +if __name__ == "__main__": + obj = Solution() + print(obj.removeSubfolders(folder=["/a", "/a/b", "/c/d", "/c/d/e", "/c/f"])) + print(obj.removeSubfolders(folder=["/a", "/a/b/c", "/a/b/d"])) + print(obj.removeSubfolders(folder=["/a/b/c", "/a/b/ca", "/a/b/d"])) |