|
| 1 | +package problems.easy; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +/** |
| 9 | + * Created by sherxon on 5/20/17. |
| 10 | + */ |
| 11 | +public class FileSystem { |
| 12 | + |
| 13 | + Node root; |
| 14 | + |
| 15 | + public FileSystem() { |
| 16 | + root = new Node(""); |
| 17 | + } |
| 18 | + |
| 19 | + public List<String> ls(String path) { |
| 20 | + List<String> list = new ArrayList<>(); |
| 21 | + ls(path.split("/"), 0, list, root); |
| 22 | + return list; |
| 23 | + } |
| 24 | + |
| 25 | + private void ls(String[] path, int i, List<String> list, Node root) { |
| 26 | + if (root == null) { |
| 27 | + return; |
| 28 | + } |
| 29 | + if (i == path.length - 1) { |
| 30 | + list.addAll(root.children.keySet()); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + ls(path, i + 1, list, root.children.get(path[i + 1])); |
| 35 | + } |
| 36 | + |
| 37 | + public void mkdir(String path) { |
| 38 | + mkdir(path.split("/"), root, 1); |
| 39 | + } |
| 40 | + |
| 41 | + private void mkdir(String[] split, Node root, int i) { |
| 42 | + if (i == split.length - 1) { |
| 43 | + return; |
| 44 | + } |
| 45 | + if (!root.children.containsKey(split[i])) { |
| 46 | + root.children.put(split[i], new Node(split[i])); |
| 47 | + } |
| 48 | + mkdir(split, root.children.get(split[i]), i + 1); |
| 49 | + } |
| 50 | + |
| 51 | + public void addContentToFile(String filePath, String content) { |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + private class Node { |
| 57 | + |
| 58 | + String name; |
| 59 | + Map<String, Node> children = new HashMap<>(); |
| 60 | + String content; |
| 61 | + |
| 62 | + public Node(String n) { |
| 63 | + this.name = n; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | +} |
0 commit comments