Design a simple file directory system that supports two main operations:
true
. Returns false
if the path already exists or if its parent directory does not exist.-1
if the path does not exist.Each path string consists of one or more segments starting with a slash (/
) followed by lowercase English letters. For example, /home
and /home/user
are valid paths, but empty strings or just /
are invalid.
Implement these two functions accordingly.
Input: ["DirectorySystem","addPath","fetch"] [[],["/x",5],["/x"]] Output: [null,true,5] Explanation: DirectorySystem dirSys = new DirectorySystem(); dirSys.addPath("/x", 5); // returns true dirSys.fetch("/x"); // returns 5
Input: ["DirectorySystem","addPath","addPath","fetch","addPath","fetch"] [[],["/dir",3],["/dir/subdir",8],["/dir/subdir"],["/a/b",2],["/a"]] Output: [null,true,true,8,false,-1] Explanation: DirectorySystem dirSys = new DirectorySystem(); dirSys.addPath("/dir", 3); // returns true dirSys.addPath("/dir/subdir", 8); // returns true dirSys.fetch("/dir/subdir"); // returns 8 dirSys.addPath("/a/b", 2); // returns false because parent "/a" does not exist dirSys.fetch("/a"); // returns -1 because path does not exist