Remove Sub | 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
def eraseSubdirectory ( dir ) : NEW_LINE INDENT res = [ ] NEW_LINE dir . sort ( ) NEW_LINE res . append ( dir [ 0 ] ) NEW_LINE print ( " { " , dir [ 0 ] , end = " , ▁ " ) NEW_LINE for i in range ( 1 , len ( dir ) ) : NEW_LINE INDENT curr = dir [ i ] NEW_LINE prev = res [ len ( res ) - 1 ] NEW_LINE l = len ( prev ) NEW_LINE if ( len ( curr ) > l and curr [ l ] == ' / ' and prev == curr [ : l ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT res . append ( curr ) NEW_LINE print ( curr , end = " , ▁ " ) NEW_LINE DEDENT print ( " } " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT dir = [ " / a " , " / a / j " , " / c / d / e " , " / c / d " , " / b " ] NEW_LINE eraseSubdirectory ( dir ) NEW_LINE DEDENT