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) */