Design a basic file system that offers two operations:
addPath(route, val):
Creates a new path with an associated integer value if it doesn't exist and its parent path exists. Returns true
if successful, otherwise false
.fetch(route):
Returns the integer value linked with the given path. Returns -1
if the path is not found.A valid path consists of one or more segments starting with a slash /
followed by lowercase letters. For example, /example
and /example/test
are valid paths, while an empty string or /
alone are invalid.
Implement these two methods to manage the file system.
addPath("/x", 5) // returns true fetch("/x") // returns 5
addPath("/abc", 3) // returns true addPath("/abc/def", 7) // returns true fetch("/abc/def") // returns 7 addPath("/xyz/123", 10) // returns false (parent path "/xyz" does not exist) fetch("/xyz") // returns -1
addPath
and fetch
will not exceed 12,000 in total.