-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1233.java
36 lines (34 loc) · 1.16 KB
/
_1233.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.fishercoder.solutions.secondthousand;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class _1233 {
public static class Solution1 {
public List<String> removeSubfolders(String[] folder) {
Arrays.sort(folder);
List<String> ans = new ArrayList<>();
Set<String> set = new HashSet<>();
for (int i = 0; i < folder.length; i++) {
String[] parts = folder[i].split("/");
StringBuilder sb = new StringBuilder();
boolean isSubFolder = false;
for (int j = 0; j < parts.length; j++) {
sb.append(parts[j]);
if (set.contains(sb.toString())) {
isSubFolder = true;
break;
}
sb.append("/");
}
if (!isSubFolder) {
sb.setLength(sb.length() - 1);
ans.add(sb.toString());
set.add(sb.toString());
}
}
return ans;
}
}
}