-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFind Selected Directories.java
115 lines (97 loc) · 3.41 KB
/
Find Selected Directories.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// https://leetcode.com/discuss/interview-question/5039879/Google-Onsite-Interview
// I was given (as strings) as directories e.g.
// /a/b/x.txt
// /a/b/p.txt
// /a/c
// /a/d/y.txt
// /a/d/z.txt
// Also, I was given the selected directories e.g.
// /a/d/y.txt
// /a/d/z.txt
// /a/b/p.txt
// My output should be
// /a/d
// /a/b/p.txt
// /a/d
// is the answer because it has 2 txt files (y and z), and both are selected.
// /a/b/p.txt
// is the answer because another file in the directory i.e. /a/b/x.txt is not selected, if it was selected, answer would have been /a/b
// Basically, if all items are selected in a particular directory, we need to return the just prev directory.
import java.util.*;
class TrieNode {
int count;
Map<String, TrieNode> children;
TrieNode() {
count = 0;
children = new HashMap<>();
}
}
class Trie {
TrieNode root;
Trie() {
root = new TrieNode();
}
void insert(String directory) {
TrieNode curr = root;
String[] path = directory.split("/");
for (int i = 0; i < path.length; i++) {
if (!curr.children.containsKey(path[i])) {
curr.children.put(path[i], new TrieNode());
}
curr.children.get(path[i]).count++;
curr = curr.children.get(path[i]);
}
}
void select(String directory) {
TrieNode curr = root;
String[] path = directory.split("/");
for (int i = 0; i < path.length; i++) {
curr.children.get(path[i]).count--;
curr = curr.children.get(path[i]);
}
}
String getSelectedDirectory(String directory) {
TrieNode curr = root;
String[] path = directory.split("/");
StringBuilder selectedDirectory = new StringBuilder();
for (int i = 0; i < path.length; i++) {
curr = curr.children.get(path[i]);
if (selectedDirectory.length() != 0) {
selectedDirectory.append("/");
}
selectedDirectory.append(path[i]);
if (curr.count == 0) {
break;
}
}
return selectedDirectory.toString();
}
}
class Solution {
List<String> getSelectedDirectories(String[] directories, String[] selectedDirectories) {
Trie trie = new Trie();
for (String directory: directories) {
trie.insert(directory);
}
for (String directory: selectedDirectories) {
trie.select(directory);
}
List<String> result = new ArrayList<>();
Set<String> resultSet = new HashSet<>();
for (String directory: selectedDirectories) {
String selection = trie.getSelectedDirectory(directory);
resultSet.add(selection);
}
for (String directory: resultSet) {
result.add(directory);
}
return result;
}
}
public class Main {
public static void main(String[] args) {
String[] directories = new String[]{"/a/b/x.txt", "/a/b/p.txt", "/a/c", "/a/d/y.txt", "/a/d/z.txt"};
String[] selectedDirectories = new String[]{"/a/d/y.txt", "/a/d/z.txt", "/a/b/p.txt"};
System.out.println((new Solution()).getSelectedDirectories(directories, selectedDirectories));
}
}