Design File System Problem
Description
LeetCode Problem 1166.
You are asked to design a file system that allows you to create new paths and associate them with different values.
The format of a path is one or more concatenated strings of the form: / followed by one or more lowercase English letters. For example, "/leetcode" and "/leetcode/problems" are valid paths while an empty string "" and "/" are not.
Implement the FileSystem class:
bool createPath(string path, int value)Creates a newpathand associates avalueto it if possible and returnstrue. Returnsfalseif the path already exists or its parent path doesn't exist.int get(string path)Returns the value associated withpathor returns-1if the path doesn't exist.
Example 1:
Input:
["FileSystem","createPath","get"]
[[],["/a",1],["/a"]]
Output:
[null,true,1]
Explanation:
FileSystem fileSystem = new FileSystem();
fileSystem.createPath("/a", 1); // return true
fileSystem.get("/a"); // return 1
Example 2:
Input:
["FileSystem","createPath","createPath","get","createPath","get"]
[[],["/leet",1],["/leet/code",2],["/leet/code"],["/c/d",1],["/c"]]
Output:
[null,true,true,2,false,-1]
Explanation:
FileSystem fileSystem = new FileSystem();
fileSystem.createPath("/leet", 1); // return true
fileSystem.createPath("/leet/code", 2); // return true
fileSystem.get("/leet/code"); // return 2
fileSystem.createPath("/c/d", 1); // return false because the parent path "/c" doesn't exist.
fileSystem.get("/c"); // return -1 because this path doesn't exist.
Constraints:
2 <= path.length <= 1001 <= value <= 10^9- Each
pathis valid and consists of lowercase English letters and'/'. - At most
10^4calls in total will be made tocreatePathandget.
Solution
Approach: Hash Map of Full Paths
Paths are always given in full, so we can store them in a hash map from the full path string to its value. No tree is needed: the parent of a path is just the text before its last /.
createPath(path, value): fail ifpathis already in the map. Its parent ispathup to its last/. If that is the empty string, the path is directly under the root, which always exists. Otherwise the parent must be in the map, or we fail. If both checks pass, storepath -> valueand returntrue.get(path): return the stored value, or-1if the path is not in the map.
Only the direct parent needs to be checked: it could only have been created if its own parent existed, and so on up to the root. Also note that "/ab" existing says nothing about "/a": the parent is found by the last /, not by a string prefix.
Complexity: O(L) time for each call, where L is the length of the path (to hash and slice it), and O(total length of all created paths) space.
class FileSystem:
def __init__(self):
self.values = {} # full path -> its value
def createPath(self, path: str, value: int) -> bool:
if path in self.values:
return False
parent = path[: path.rfind("/")] # "" when the path has one part: its parent is the root
if parent and parent not in self.values:
return False
self.values[path] = value
return True
def get(self, path: str) -> int:
return self.values.get(path, -1)