classMapSum:def__init__(self):""" Initialize your data structure here. """self.data=defaultdict(int)self.t=defaultdict(int)definsert(self,key:str,val:int)->None:old=self.t[key]self.t[key]=valforiinrange(1,len(key)+1):self.data[key[:i]]+=val-olddefsum(self,prefix:str)->int:returnself.data[prefix]# Your MapSum object will be instantiated and called as such:# obj = MapSum()# obj.insert(key,val)# param_2 = obj.sum(prefix)
classMapSum{privateMap<String,Integer>data;privateMap<String,Integer>t;/** Initialize your data structure here. */publicMapSum(){data=newHashMap<>();t=newHashMap<>();}publicvoidinsert(Stringkey,intval){intold=t.getOrDefault(key,0);t.put(key,val);for(inti=1;i<key.length()+1;++i){Stringk=key.substring(0,i);data.put(k,data.getOrDefault(k,0)+(val-old));}}publicintsum(Stringprefix){returndata.getOrDefault(prefix,0);}}/** * Your MapSum object will be instantiated and called as such: * MapSum obj = new MapSum(); * obj.insert(key,val); * int param_2 = obj.sum(prefix); */
classMapSum{public:unordered_map<string,int>data;unordered_map<string,int>t;/** Initialize your data structure here. */MapSum(){}voidinsert(stringkey,intval){intold=t[key];t[key]=val;for(inti=1;i<key.size()+1;++i){stringk=key.substr(0,i);data[k]+=(val-old);}}intsum(stringprefix){returndata[prefix];}};/** * Your MapSum object will be instantiated and called as such: * MapSum* obj = new MapSum(); * obj->insert(key,val); * int param_2 = obj->sum(prefix); */
typeMapSumstruct{datamap[string]inttmap[string]int}/** Initialize your data structure here. */funcConstructor()MapSum{returnMapSum{data:make(map[string]int),t:make(map[string]int),}}func(this*MapSum)Insert(keystring,valint){old:=this.t[key]this.t[key]=valfori:=1;i<len(key)+1;i++{k:=key[:i]this.data[k]+=(val-old)}}func(this*MapSum)Sum(prefixstring)int{returnthis.data[prefix]}/** * Your MapSum object will be instantiated and called as such: * obj := Constructor(); * obj.Insert(key,val); * param_2 := obj.Sum(prefix); */