[Medium] 1233. Remove Sub-Folders from the Filesystem
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.
The format of a path is one or more concatenated strings of the form: '/' followed by one or more lowercase English letters.
For example, "/leetcode" and "/leetcode/problems" are valid paths while an empty string and "/" are not.
Examples
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"]
Explanation: None of the folders are subfolders of another folder.
Constraints
1 <= folder.length <= 4 * 10^42 <= folder[i].length <= 100folder[i]contains only lowercase letters and'/'folder[i]always starts with'/'- Each folder name is unique
Thinking Process
- Prefix Matching: A folder is a subfolder if its path is a prefix of another folder
- Efficient for checking if any prefix exists
- More memory intensive but clearer logic
- Simpler code, more efficient
- Strings often need frequency maps or two-pointer scans.
- Watch index bounds and empty-string edge cases.
- Stack helps with nested or repeated patterns.
Common Approaches
Typical techniques for this pattern:
| Approach | Time | Space | Notes |
|---|---|---|---|
| Two pointers on string (this problem) | O(n) | O(1) | Palindrome, parsing |
| Hash map / frequency | O(n) | O(k) | Anagram, character counts |
| KMP / rolling hash | O(n) | O(n) | Pattern matching |
| Stack parsing | O(n) | O(n) | Decode string, parentheses |
Solution
class TrieNode{
boolean isEnd;
unordered_map<String, TrieNode*> children;
TrieNode() {}
}
class Solution {
Solution() {}
String[]removeSubfolders(String[] folder) {
for (int path : folder) {
TrieNode curr = root;
istringstream iss = new istringstream(path);
String folderName;
while(getline(iss, folderName, '/')) {
if(folderName.length == 0) continue;
if(!curr.children.contains(folderName)) {
curr.children[folderName] = new TrieNode();
}
curr = curr.children[folderName];
}
curr.isEnd = true;
}
String[]rtn;
for (int path : folder) {
TrieNode curr = root;
istringstream iss = new istringstream(path);
String folderName;
boolean isSubFolder = false;
while(getline(iss, folderName, '/')) {
if(folderName.length == 0) continue;
var it = curr.children.find(folderName);
if(it == curr.children.iterator()) break;
TrieNode nextNode = it[1];
if(nextNode.isEnd && iss.rdbuf().in_avail() != 0) {
isSubFolder = true;
break;
}
curr = nextNode;
}
if(!isSubFolder) rtn.add(path);
}
return rtn;
}
TrieNode root;
public void deleteTrie(TrieNode node) {
if(!node) return;
for(auto& [_, child]: node.children) {
deleteTrie(child);
}
delete node;
}
}
Solution Explanation
Approach: Two pointers on string (this problem)
Key idea: 1. Prefix Matching: A folder is a subfolder if its path is a prefix of another folder
How the code works:
- Prefix Matching: A folder is a subfolder if its path is a prefix of another folder
- Efficient for checking if any prefix exists
- More memory intensive but clearer logic
- Simpler code, more efficient
- Strings often need frequency maps or two-pointer scans.
- Watch index bounds and empty-string edge cases.
Walkthrough — input folder = ["/a","/a/b","/c/d","/c/d/e","/c/f"], expected output ["/a","/c/d","/c/f"]:
Folders “/a/b” is a subfolder of “/a” and “/c/d/e” is inside of folder “/c/d” in our filesystem.
Common Mistakes
- Single folder:
["/a"]→["/a"] - No subfolders:
["/a/b", "/c/d"]→["/a/b", "/c/d"] - All subfolders:
["/a", "/a/b", "/a/b/c"]→["/a"] - Similar prefixes:
["/a/b", "/a/bc"]→ Both kept (not subfolders) -
Deep nesting:
["/a", "/a/b", "/a/b/c", "/a/b/c/d"]→["/a"] - False prefix match:
/a/bcmatching/a/bwithout checking next character - Wrong comparison: Using
startsWithwithout verifying/boundary - Memory leaks: Not deleting trie nodes in C++
- Empty string handling: Not skipping empty strings from leading
/ - Sorting order: Not understanding lexicographic ordering
Related Problems
- LC 208: Implement Trie (Prefix Tree) - Trie basics
- LC 648: Replace Words - Trie for prefix replacement
- LC 720: Longest Word in Dictionary - Trie traversal
- LC 14: Longest Common Prefix - Prefix matching
Key Takeaways
- Prefix Matching: A folder is a subfolder if its path is a prefix of another folder
- Trie Approach:
- Efficient for checking if any prefix exists
- More memory intensive but clearer logic
- Sorting Approach:
- Simpler code, more efficient
- Leverages lexicographic ordering property
- Path Parsing: Use
istringstreamandgetlineto split by/ - Edge Case: Check that prefix match is followed by
/to avoid false positives
References
- LC 1233: Remove Sub-Folders from the Filesystem on LeetCode
- LeetCode Discuss — LC 1233: Remove Sub-Folders from the Filesystem
- LeetCode Editorial (may require premium)