Skip to content

1233. Remove Sub-Folders from the Filesystem #747

Answered by kovatz
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can utilize a combination of sorting and string comparison. The steps below outline a solution in PHP:

  1. Sort the Folders Lexicographically: Sorting the folder paths in lexicographical order ensures that any sub-folder will follow its parent folder immediately. For example, "/a" will be followed by "/a/b" in the sorted list, allowing us to check for sub-folder relationships easily.

  2. Identify and Filter Out Sub-Folders: We can iterate through the sorted list, checking if the current folder path is a sub-folder of the previously added path. If it is, we skip it. If not, we add it to our result list.

  3. Implement the Solution in PHP: We keep track of the last folder path added to the resu…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Oct 25, 2024
Maintainer Author

@kovatz
Comment options

kovatz Oct 25, 2024
Collaborator

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty hacktoberfest hacktoberfest hacktoberfest-accepted hacktoberfest accepted
2 participants