-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path677.py
33 lines (24 loc) · 863 Bytes
/
677.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Problem link: https://leetcode.com/problems/map-sum-pairs/
class MapSum:
def __init__(self):
self.wordValue = dict()
self.prefixSum = dict()
def insert(self, key: str, val: int) -> None:
toAdd = val
if key in self.wordValue:
toAdd = val - self.wordValue[key]
self.wordValue[key] = val
prefix = ''
for letter in key:
prefix += letter
if prefix not in self.prefixSum:
self.prefixSum[prefix] = 0
self.prefixSum[prefix] += toAdd
def sum(self, prefix: str) -> int:
if prefix not in self.prefixSum:
return 0
return self.prefixSum[prefix]
# Your MapSum object will be instantiated and called as such:
# obj = MapSum()
# obj.insert(key,val)
# param_2 = obj.sum(prefix)