Skip to content

Commit 7fa9a8b

Browse files
authored
Create RemoveSubFolders.java
This solution sorts the folders and then iterates through them, ensuring that only the top-level folders are added to the result list.
1 parent bc9645c commit 7fa9a8b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
public class RemoveSubFolders {
9+
10+
public static List<String> removeSubfolders(String[] folder) {
11+
Arrays.sort(folder);
12+
List<String> result = new ArrayList<>();
13+
String prev = " ";
14+
for (String f : folder) {
15+
if (!f.startsWith(prev)) {
16+
result.add(f);
17+
prev = f + "/";
18+
}
19+
}
20+
return result;
21+
}
22+
23+
public static void main(String[] args) {
24+
String[] folder = {"/a","/a/b","/c/d","/c/d/e","/c/f"};
25+
List<String> result = removeSubfolders(folder);
26+
System.out.println(result); // Output: ["/a", "/c/d", "/c/f"]
27+
}
28+
}

0 commit comments

Comments
 (0)