1233. Remove Sub-Folders from the Filesystem LeetCode Solution

In this guide, you will get 1233. Remove Sub-Folders from the Filesystem LeetCode Solution with the best time and space complexity. The solution to Remove Sub-Folders from the Filesystem problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Remove Sub-Folders from the Filesystem solution in C++
  4. Remove Sub-Folders from the Filesystem solution in Java
  5. Remove Sub-Folders from the Filesystem solution in Python
  6. Additional Resources
1233. Remove Sub-Folders from the Filesystem LeetCode Solution image

Problem Statement of 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. A sub-folder of folder[j] must start with folder[j], followed by a “/”. For example, “/a/b” is a sub-folder of “/a”, but “/b” is not a sub-folder of “/a/b/c”.
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.

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”]

Constraints:

1 <= folder.length <= 4 * 104
2 <= folder[i].length <= 100
folder[i] contains only lowercase letters and '/'.
folder[i] always starts with the character '/'.
Each folder name is unique.

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(n)

1233. Remove Sub-Folders from the Filesystem LeetCode Solution in C++

class Solution {
 public:
  vector<string> removeSubfolders(vector<string>& folder) {
    vector<string> ans;
    string prev;

    ranges::sort(folder);

    for (const string& f : folder) {
      if (!prev.empty() && f.find(prev) == 0 && f[prev.length()] == '/')
        continue;
      ans.push_back(f);
      prev = f;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1233. Remove Sub-Folders from the Filesystem LeetCode Solution in Java

class Solution {
  public List<String> removeSubfolders(String[] folder) {
    List<String> ans = new ArrayList<>();
    String prev = "";

    Arrays.sort(folder);

    for (final String f : folder) {
      if (!prev.isEmpty() && f.startsWith(prev) && f.charAt(prev.length()) == '/')
        continue;
      ans.add(f);
      prev = f;
    }

    return ans;
  }
}
// code provided by PROGIEZ

1233. Remove Sub-Folders from the Filesystem LeetCode Solution in Python

class Solution:
  def removeSubfolders(self, folder: list[str]) -> list[str]:
    ans = []
    prev = ""

    folder.sort()

    for f in folder:
      if len(prev) > 0 and f.startswith(prev) and f[len(prev)] == '/':
        continue
      ans.append(f)
      prev = f

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.