-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpathToRootFolder.java
More file actions
124 lines (105 loc) · 3.22 KB
/
pathToRootFolder.java
File metadata and controls
124 lines (105 loc) · 3.22 KB
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
116
117
118
119
120
121
122
123
124
import java.io.*;
import java.util.*;
/*
Given list of folders, print the path of a given folder from root. If there is no path to the root folder then return an empty string. Root level folders will have 0 as id. The structure of Folder is like this:
class Folder {
int id;
List<int> subfolders;
String name;
}
Ex:
list =
[Folder(0, [7, 3], “abc”),
Folder(0, [], “xyz”),
Folder(8, [9], “def”),
Folder(7, [], “ijk),
Folder(9, [], “lmn”)]
printPath(9) = “abc” -> “ijk” -> “lmn” printPath(8) = ""
Clarification: There can be multiple root level folders. All other folders have unique ids.
Note: printPath method may be called multiple times. Design your solution taking that into account
0: abc
0: xyz
idToFold
8: def
7: ijk
9: lmn
*/
// Main class should be named 'Solution'
class Folder {
int id;
int[] subfolders;
String name;
Folder(int id, int[] sub, String name)
{
this.id = id;
subfolders = sub;
this.name = name;
}
@Override
public int hashCode()
{
// uses roll no to verify the uniqueness
// of the object of Student class
final int temp = 14;
int ans = 1;
ans = temp * ans + id;
return ans;
}
@Override
public boolean equals(Object o)
{
Folder f = (Folder) o;
return this.id == f.id;
}
}
class Solution {
public static void main(String[] args) {
List<Folder> list = new ArrayList();
list.add(new Folder(0, new int[]{7, 3}, "abc"));
list.add(new Folder(0, new int[]{8}, "xyz"));
list.add(new Folder(8, new int[]{}, "def"));
list.add(new Folder(7, new int[]{9}, "ijk"));
list.add(new Folder(9, new int[]{7}, "lmn"));
System.out.println(printPath(list, 9));
}
public static String printPath(List<Folder> folders, int fId)
{
HashMap<Integer,Folder> idToFold = new HashMap<>();
for(Folder fold : folders)
{
if(fold.id != 0)
idToFold.put(fold.id, fold);
}
HashMap<Folder, Folder> map = new HashMap<>();
for(Folder root : folders)
{
for(int folder : root.subfolders)
{
if(idToFold.containsKey(folder)){
map.put(idToFold.get(folder), root);
}
}
}
StringBuilder path = new StringBuilder();
Folder cur = idToFold.get(fId);
if(cur == null) return "Invalid input folder";
path.append(cur.name);
//list = [Folder(0, [7, 3], “abc”), Folder(0, [8], “xyz”), Folder(8, [], “def”), Folder(7, [9], “ijk), Folder(9, [8], “lmn”)]
HashSet<Folder> visited = new HashSet<>();
while(cur.id != 0)
{
Folder parent = map.get(cur);
if(parent!=null && !visited.contains(parent))
{
visited.add(parent);
path.append("-->");
path.append(parent.name);
cur = parent;
}
else {
return "No path to root!";
}
}
return path.toString();
}
}