
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Remove Sub-Folders from the Filesystem in C++
Suppose we have a list of folders, we have to remove all sub-folders in those folders and return in any order the folders after removing. Here if a folder[i] is located within another folder[j], it is denoted as subfolder of it. The paths will be like folder1/subfolder2/… etc.
Suppose the input is like
["/myfolder","/myfolder/secondfolder","/another/document","/another/document/extrafolder","/another/final"], then the output will be: ["/myfolder","/another/final","/another/document"]
To solve this, we will follow these steps −
- sort the folder array based on the length of the paths
- create one map m, and another array ans
- for i in range 0 to size of path array – 1
- s := path_array[i]
- temp := empty string
- set flag as true
- for j in range 0 to size of s
- temp := temp + s[j]
- increase j by 1
- while j < size of array and s[j] is not ‘/’
- temp := temp + s[j], and increase j by 1
- if m[temp] is not false, then flag := false, and break
- if flag is true, then insert s into ans, and set m[s] := true
- return ans
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; void print_vector(vector<auto> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class Solution { public: static bool cmp(string s,string x){ return s.size()<x.size(); } vector<string> removeSubfolders(vector<string>& f) { sort(f.begin(),f.end(),cmp); map <string,bool> m; vector <string> ans; for(int i =0;i<f.size();i++){ string s= f[i]; string temp=""; bool flag = true; for(int j =0;j<s.size();){ temp+=s[j]; j++; while(j<s.size() && s[j]!='/'){ temp+=s[j]; j++; } if(m[temp]){ flag = false; break; } } if(flag){ ans.push_back(s); m[s]=true; } } return ans; } }; main(){ vector<string> v = {"/myfolder","/myfolder/secondfolder","/another/document","/another/document/extrafolder","/another/final"}; Solution ob; print_vector(ob.removeSubfolders(v)); }
Input
["/myfolder","/myfolder/secondfolder","/another/document","/another/document/extrafolder","/another/final"]
Output
[/myfolder, /another/final, /another/document, ]
- Related Articles
- Upload from local drive to local filesystem in HTML with Filesystem API
- How to delete folder and sub folders using Java?
- MongoDB query to remove element from array as sub property
- How to remove hidden files and folders using Python?
- How to delete all files and folders from a path in C#?
- Remove all elements from the ArrayList in C#
- Remove all elements from the SortedSet in C#
- Remove all entries from the ListDictionary in C#
- Remove all objects from the Queue in C#
- Remove all elements from the Collection in C#
- Remove all elements from the Hashtable in C#
- Remove all objects from the Stack in C#
- Remove the first occurrence from the StringCollection in C#
- Remove all the strings from the StringCollection in C#
- Remove the specified element from a HashSet in C#

Advertisements