Remove Sub | C ++ program for the above approach ; Function to remove sub - directories from the given lists dir ; Store final result ; Sort the given directories ; Insert 1 st directory ; Iterating in directory ; Current directory ; Our previous valid directory ; Find length of previous directory ; If subdirectory is found ; Else store it in result valid directory ; Driver Code ; Given lists of directories dir [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void eraseSubdirectory ( vector < string > & dir ) { vector < string > res ; sort ( dir . begin ( ) , dir . end ( ) ) ; res . push_back ( dir [ 0 ] ) ; cout << " { " << dir [ 0 ] << " , ▁ " ; for ( int i = 1 ; i < dir . size ( ) ; i ++ ) { string curr = dir [ i ] ; string prev = res . back ( ) ; int l = prev . length ( ) ; if ( curr . length ( ) > l && curr [ l ] == ' / ' && prev == curr . substr ( 0 , l ) ) continue ; res . push_back ( curr ) ; cout << curr << " , ▁ " ; } cout << " } STRNEWLINE " ; } int main ( ) { vector < string > dir = { " / a " , " / a / j " , " / c / d / e " , " / c / d " , " / b " } ; eraseSubdirectory ( dir ) ; return 0 ; }