Skip to content

Commit 0cfdf46

Browse files
committed
Jul 19
1 parent 74d7005 commit 0cfdf46

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

2025-07-July-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
| July 16 | [3201. Find the Maximum Length of Valid Subsequence I](https://leetcode.com/problems/find-the-maximum-length-of-valid-subsequence-i/) | Medium | Solved |
2323
| July 17 | [3202. Find the Maximum Length of Valid Subsequence II](https://leetcode.com/problems/find-the-maximum-length-of-valid-subsequence-ii/) | Medium | Unsolved |
2424
| July 18 | [2163. Minimum Difference in Sums After Removal of Elements](https://leetcode.com/problems/minimum-difference-in-sums-after-removal-of-elements/) | Hard | Unsolved |
25-
| July 19 | []() | | |
25+
| July 19 | [1233. Remove Sub-Folders from the Filesystem](https://leetcode.com/problems/remove-sub-folders-from-the-filesystem/) | Medium | Solved |
2626
| July 20 | []() | | |
2727
| July 21 | []() | | |
2828
| July 22 | []() | | |
@@ -41,5 +41,5 @@
4141
| Level | Problems | Solved | Unsolved |
4242
| --- | --- | --- | --- |
4343
| Easy | 5 | 5 | 0 |
44-
| Medium | 7 | 6 | 1 |
44+
| Medium | 8 | 7 | 1 |
4545
| Hard | 6 | 1 | 5 |
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from typing import Generator
2+
3+
4+
class TrieNode:
5+
def __init__(self):
6+
self.children = {}
7+
self.is_end = False
8+
9+
10+
class Trie:
11+
def __init__(self):
12+
self.root = TrieNode()
13+
14+
def insert(self, path: str) -> None:
15+
node = self.root
16+
for folder in path.split('/')[1:]:
17+
node = node.children.setdefault(folder, TrieNode())
18+
if node.is_end:
19+
return
20+
node.is_end = True
21+
node.children.clear()
22+
23+
def collect(self, node: TrieNode, path: str = '') -> Generator[str, None, None]:
24+
if node.is_end:
25+
yield path
26+
for folder, child in node.children.items():
27+
yield from self.collect(child, f'{path}/{folder}')
28+
29+
30+
class Solution:
31+
def removeSubfolders(self, folder: list[str]) -> list[str]:
32+
trie = Trie()
33+
for path in folder:
34+
trie.insert(path)
35+
return list(trie.collect(trie.root))
36+
37+
38+
def main():
39+
folders = ['/a', '/a/b', '/c/d', '/c/d/e', '/c/f']
40+
assert Solution().removeSubfolders(folders) == ['/a', '/c/d', '/c/f']
41+
42+
folders = ['/a', '/a/b/c', '/a/b/d']
43+
assert Solution().removeSubfolders(folders) == ['/a']
44+
45+
folders = ['/a/b/c', '/a/b/d', '/a/b/c/e']
46+
assert Solution().removeSubfolders(folders) == ['/a/b/c', '/a/b/d']
47+
48+
49+
if __name__ == '__main__':
50+
main()

0 commit comments

Comments
 (0)