πΉ Problem: 1233. Remove Sub-Folders from the Filesystem
Difficulty: #Medium
Tags: #String, #Sorting
π Problem Summary
Youβre given a list of folder paths (e.g., ["/a","/a/b","/c/d","/c/d/e","/c/f"]
).
The task is to remove all sub-folders, keeping only the top-level parent folders.
A folder
f1
is a sub-folder off2
iff1
starts withf2 + "/"
.
Return a list of folders excluding all sub-folders.
π§ My Thought Process
Brute Force Idea:
For each folder, check if it's a subfolder of any other folder in the list. That would beO(nΒ²)
comparisons. Not scalable.-
Optimized Strategy:
Sort the folders lexicographically. That way, any subfolder will immediately follow its parent.- Initialize the result list with the first folder.
- For every folder from index
1
onward: - If it doesnβt start with the last added folder +
'/'
, it's a top-level folder β add it to result.
Algorithm Used:
Sorting + Prefix Check
βοΈ Code Implementation (Python)
class Solution:
def removeSubfolders(self, folder: List[str]) -> List[str]:
folder.sort() # Lexicographically sort paths
res = [folder[0]] # First one is always a root
for f in folder[1:]:
prev = res[-1]
# Check if `f` is NOT a subfolder of the last added root
if not (f.startswith(prev) and len(f) > len(prev) and f[len(prev)] == "/"):
res.append(f)
return res
β±οΈ Time & Space Complexity
-
Time:
O(n log n)
β Sorting takesn log n
, rest is linear scan -
Space:
O(n)
β Result list
π§© Key Takeaways
- β Learned how lexicographic sorting simplifies hierarchical string problems.
- π‘ Tricky part was making sure we don't falsely match
/a/bc
as subfolder of/a
. - π Will look out for prefix-matching problems where sorted input helps reduce complexity.
π Reflection (Self-Check)
- [x] Could I solve this without help?
- [x] Did I write code from scratch?
- [x] Did I understand why it works?
- [x] Will I be able to recall this in a week?
π Progress Tracker
Metric | Value |
---|---|
Day | 48 |
Total Problems Solved | 389 |
Confidence Today | π |
Leetcode Rating | 1572 |
Top comments (0)