|
| 1 | +package problemsolving; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +/** |
| 9 | + * Find all Labels from given folders |
| 10 | + * |
| 11 | + * @author Manjunath Asundi |
| 12 | + */ |
| 13 | +public class BuildLabels { |
| 14 | + |
| 15 | + /** |
| 16 | + * Folder |
| 17 | + */ |
| 18 | + static class Folder { |
| 19 | + int id; |
| 20 | + String displayName; |
| 21 | + int parentId; |
| 22 | + |
| 23 | + Folder(int id, String displayName, int parentId) { |
| 24 | + this.id = id; |
| 25 | + this.displayName = displayName; |
| 26 | + this.parentId = parentId; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Label |
| 32 | + */ |
| 33 | + static class Label { |
| 34 | + String displayName; |
| 35 | + |
| 36 | + Label(String displayName) { |
| 37 | + this.displayName = displayName; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public static List<Label> buildLabels(List<Folder> fList) { |
| 42 | + Map<Integer, Folder> rootChildMap = new HashMap<>(); |
| 43 | + List<Label> labels = new ArrayList<>(); |
| 44 | + for (int i = 0; i < fList.size(); i++) { |
| 45 | + for (int j = 0; j < fList.size(); j++) { |
| 46 | + if (fList.get(i).parentId == fList.get(j).id) |
| 47 | + rootChildMap.put(fList.get(i).id, fList.get(j)); |
| 48 | + } |
| 49 | + } |
| 50 | + Map<Integer, String> resultMap = new HashMap<>(); |
| 51 | + for (int i = 0; i < fList.size(); i++) { |
| 52 | + Folder folder = fList.get(i); |
| 53 | + String displayName = ""; |
| 54 | + if (folder.parentId != -1) { |
| 55 | + displayName = buildLabelsUtil(rootChildMap, folder, displayName, resultMap); |
| 56 | + resultMap.put(folder.id, displayName); |
| 57 | + } else { |
| 58 | + displayName = folder.displayName; |
| 59 | + resultMap.put(folder.id, folder.displayName); |
| 60 | + } |
| 61 | + labels.add(new Label(displayName)); |
| 62 | + } |
| 63 | + return labels; |
| 64 | + } |
| 65 | + |
| 66 | + public static String buildLabelsUtil(Map<Integer, Folder> rootChildMap, Folder folder, String displayName, |
| 67 | + Map<Integer, String> resultMap) { |
| 68 | + if (folder.parentId == -1) |
| 69 | + return folder.displayName; |
| 70 | + if (resultMap.containsKey(folder.id)) |
| 71 | + return resultMap.get(folder.id); |
| 72 | + String resultString = displayName + folder.displayName; |
| 73 | + String temp = buildLabelsUtil(rootChildMap, rootChildMap.get(folder.id), displayName, resultMap); |
| 74 | + if (temp != null) |
| 75 | + resultString = temp + "/" + resultString; |
| 76 | + if (!resultMap.containsKey(folder.id)) |
| 77 | + resultMap.put(folder.id, resultString); |
| 78 | + return resultString; |
| 79 | + } |
| 80 | + |
| 81 | + public static void main(String[] args) { |
| 82 | + List<Folder> folderList = new ArrayList<Folder>(); |
| 83 | + folderList.add(new Folder(1, "A", -1)); |
| 84 | + folderList.add(new Folder(2, "B", 1)); |
| 85 | + folderList.add(new Folder(3, "C", 1)); |
| 86 | + folderList.add(new Folder(4, "D", 2)); |
| 87 | + folderList.add(new Folder(5, "E", 3)); |
| 88 | + List<Label> buildLabels = buildLabels(folderList); |
| 89 | + for (Label label : buildLabels) { |
| 90 | + System.out.println(label.displayName); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments