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 new path and associates a value to it if possible and returns true. Returns false if the path already exists or its parent path doesn't exist.
int get(string path) Returns the value associated with path or returns -1 if the path doesn't exist.
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 <= 100
1 <= value <= 109
Each path is valid and consists of lowercase English letters and '/'.
At most 104 calls in total will be made to createPath and get.
Solutions
Solution 1: Trie
We can use a trie to store the paths, where each node stores a value, representing the value of the path corresponding to the node.
The structure of the trie node is defined as follows:
children: Child nodes, stored in a hash table, where the key is the path of the child node, and the value is the reference to the child node.
v: The value of the path corresponding to the current node.
The methods of the trie are defined as follows:
insert(w, v): Insert the path $w$ and set its corresponding value to $v$. If the path $w$ already exists or its parent path does not exist, return false, otherwise return true. The time complexity is $O(|w|)$, where $|w|$ is the length of the path $w$.
search(w): Return the value corresponding to the path $w$. If the path $w$ does not exist, return $-1$. The time complexity is $O(|w|)$.
The total time complexity is $O(\sum_{w \in W}|w|)$, and the total space complexity is $O(\sum_{w \in W}|w|)$, where $W$ is the set of all inserted paths.
classTrie:def__init__(self,v:int=-1):self.children={}self.v=vdefinsert(self,w:str,v:int)->bool:node=selfps=w.split("/")forpinps[1:-1]:ifpnotinnode.children:returnFalsenode=node.children[p]ifps[-1]innode.children:returnFalsenode.children[ps[-1]]=Trie(v)returnTruedefsearch(self,w:str)->int:node=selfforpinw.split("/")[1:]:ifpnotinnode.children:return-1node=node.children[p]returnnode.vclassFileSystem:def__init__(self):self.trie=Trie()defcreatePath(self,path:str,value:int)->bool:returnself.trie.insert(path,value)defget(self,path:str)->int:returnself.trie.search(path)# Your FileSystem object will be instantiated and called as such:# obj = FileSystem()# param_1 = obj.createPath(path,value)# param_2 = obj.get(path)
classTrie{Map<String,Trie>children=newHashMap<>();intv;Trie(intv){this.v=v;}booleaninsert(Stringw,intv){Trienode=this;varps=w.split("/");for(inti=1;i<ps.length-1;++i){varp=ps[i];if(!node.children.containsKey(p)){returnfalse;}node=node.children.get(p);}if(node.children.containsKey(ps[ps.length-1])){returnfalse;}node.children.put(ps[ps.length-1],newTrie(v));returntrue;}intsearch(Stringw){Trienode=this;varps=w.split("/");for(inti=1;i<ps.length;++i){varp=ps[i];if(!node.children.containsKey(p)){return-1;}node=node.children.get(p);}returnnode.v;}}classFileSystem{privateTrietrie=newTrie(-1);publicFileSystem(){}publicbooleancreatePath(Stringpath,intvalue){returntrie.insert(path,value);}publicintget(Stringpath){returntrie.search(path);}}/** * Your FileSystem object will be instantiated and called as such: * FileSystem obj = new FileSystem(); * boolean param_1 = obj.createPath(path,value); * int param_2 = obj.get(path); */
classTrie{public:unordered_map<string,Trie*>children;intv;Trie(intv){this->v=v;}boolinsert(string&w,intv){Trie*node=this;autops=split(w,'/');for(inti=1;i<ps.size()-1;++i){autop=ps[i];if(!node->children.count(p)){returnfalse;}node=node->children[p];}if(node->children.count(ps.back())){returnfalse;}node->children[ps.back()]=newTrie(v);returntrue;}intsearch(string&w){Trie*node=this;autops=split(w,'/');for(inti=1;i<ps.size();++i){autop=ps[i];if(!node->children.count(p)){return-1;}node=node->children[p];}returnnode->v;}private:vector<string>split(string&s,chardelim){stringstreamss(s);stringitem;vector<string>res;while(getline(ss,item,delim)){res.emplace_back(item);}returnres;}};classFileSystem{public:FileSystem(){trie=newTrie(-1);}boolcreatePath(stringpath,intvalue){returntrie->insert(path,value);}intget(stringpath){returntrie->search(path);}private:Trie*trie;};/** * Your FileSystem object will be instantiated and called as such: * FileSystem* obj = new FileSystem(); * bool param_1 = obj->createPath(path,value); * int param_2 = obj->get(path); */
typetriestruct{childrenmap[string]*trievint}funcnewTrie(vint)*trie{return&trie{map[string]*trie{},v}}func(t*trie)insert(wstring,vint)bool{node:=tps:=strings.Split(w,"/")for_,p:=rangeps[1:len(ps)-1]{if_,ok:=node.children[p];!ok{returnfalse}node=node.children[p]}if_,ok:=node.children[ps[len(ps)-1]];ok{returnfalse}node.children[ps[len(ps)-1]]=newTrie(v)returntrue}func(t*trie)search(wstring)int{node:=tps:=strings.Split(w,"/")for_,p:=rangeps[1:]{if_,ok:=node.children[p];!ok{return-1}node=node.children[p]}returnnode.v}typeFileSystemstruct{trie*trie}funcConstructor()FileSystem{returnFileSystem{trie:newTrie(-1)}}func(this*FileSystem)CreatePath(pathstring,valueint)bool{returnthis.trie.insert(path,value)}func(this*FileSystem)Get(pathstring)int{returnthis.trie.search(path)}/** * Your FileSystem object will be instantiated and called as such: * obj := Constructor(); * param_1 := obj.CreatePath(path,value); * param_2 := obj.Get(path); */
classTrie{children:Map<string,Trie>;v:number;constructor(v:number){this.children=newMap<string,Trie>();this.v=v;}insert(w:string,v:number):boolean{letnode:Trie=this;constps=w.split('/').slice(1);for(leti=0;i<ps.length-1;++i){constp=ps[i];if(!node.children.has(p)){returnfalse;}node=node.children.get(p)!;}if(node.children.has(ps[ps.length-1])){returnfalse;}node.children.set(ps[ps.length-1],newTrie(v));returntrue;}search(w:string):number{letnode:Trie=this;constps=w.split('/').slice(1);for(constpofps){if(!node.children.has(p)){return-1;}node=node.children.get(p)!;}returnnode.v;}}classFileSystem{trie:Trie;constructor(){this.trie=newTrie(-1);}createPath(path:string,value:number):boolean{returnthis.trie.insert(path,value);}get(path:string):number{returnthis.trie.search(path);}}/** * Your FileSystem object will be instantiated and called as such: * var obj = new FileSystem() * var param_1 = obj.createPath(path,value) * var param_2 = obj.get(path) */