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
using System ; using System . Collections ; class GFG { static void eraseSubdirectory ( ArrayList dir ) { ArrayList res = new ArrayList ( ) ; dir . Sort ( ) ; res . Add ( dir [ 0 ] ) ; Console . Write ( " { " + dir [ 0 ] + " , ▁ " ) ; for ( int i = 1 ; i < dir . Count ; i ++ ) { string curr = ( string ) dir [ i ] ; string prev = ( string ) res [ ( res . Count - 1 ) ] ; int l = prev . Length ; if ( curr . Length > l && curr [ l ] == ' / ' && prev . Equals ( curr . Substring ( 0 , l ) ) ) continue ; res . Add ( curr ) ; Console . Write ( curr + " , ▁ " ) ; } Console . Write ( " } STRNEWLINE " ) ; } public static void Main ( ) { ArrayList dir = new ArrayList ( ) { " / a " , " / a / j " , " / c / d / e " , " / c / d " , " / b " } ; eraseSubdirectory ( dir ) ; } }