LeetCode 1233: Remove Sub-Folders from the Filesystem Solution

Master LeetCode problem 1233 (Remove Sub-Folders from the Filesystem), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1233. Remove Sub-Folders from the Filesystem

Problem Explanation

Explanation

To solve this problem, we can use a hash set to store all the unique folders after removing the sub-folders. We will iterate through the input folders and for each folder, we will check if any of its parent folders are already present in the hash set. If not, we add the folder to the hash set.

Solution Code

class Solution {
    public List<String> removeSubfolders(String[] folder) {
        Arrays.sort(folder);
        Set<String> set = new HashSet<>();
        List<String> result = new ArrayList<>();
        
        for (String f : folder) {
            boolean isSubFolder = false;
            for (int i = 2; i < f.length(); i++) {
                if (f.charAt(i) == '/' && set.contains(f.substring(0, i))) {
                    isSubFolder = true;
                    break;
                }
            }
            if (!isSubFolder) {
                set.add(f);
                result.add(f);
            }
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1233 (Remove Sub-Folders from the Filesystem)?

This page provides optimized solutions for LeetCode problem 1233 (Remove Sub-Folders from the Filesystem) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1233 (Remove Sub-Folders from the Filesystem)?

The time complexity for LeetCode 1233 (Remove Sub-Folders from the Filesystem) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1233 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1233 in Java, C++, or Python.

Back to LeetCode Solutions