|
| 1 | +from typing import Dict |
| 2 | +from collections import Counter |
| 3 | + |
| 4 | +from datastructures.trees.trie import TrieNode |
| 5 | + |
| 6 | + |
| 7 | +class MapSumBruteForce(object): |
| 8 | + """ |
| 9 | + This solution to creating a map sum data structure that finds the sum of keys with a matching prefix uses a |
| 10 | + Hash Table combined with Brute-Force Search and String Matching. |
| 11 | +
|
| 12 | + Time Complexity: Every insert operation is O(1). Every sum operation is O(N*P) where N is the number of items in the |
| 13 | + map, and P is the length of the input prefix. |
| 14 | +
|
| 15 | + Space Complexity: The space used by map is linear in the size of all input key and val values combined. |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__(self): |
| 19 | + self.mapping: Dict[str, int] = {} |
| 20 | + |
| 21 | + def insert(self, key: str, val: int) -> None: |
| 22 | + """ |
| 23 | + Inserts the key with the given value into the hash table |
| 24 | + Args: |
| 25 | + key (str): key to insert |
| 26 | + val (int): value to insert |
| 27 | + """ |
| 28 | + self.mapping[key] = val |
| 29 | + |
| 30 | + def sum(self, prefix: str) -> int: |
| 31 | + """ |
| 32 | + Finds the sum of all keys with the prefix `prefix`. |
| 33 | + Args: |
| 34 | + prefix (str): prefix to search for |
| 35 | + Returns: |
| 36 | + int: sum of all keys with the prefix `prefix` |
| 37 | + """ |
| 38 | + running_sum = 0 |
| 39 | + for k, v in self.mapping.items(): |
| 40 | + if k.startswith(prefix): |
| 41 | + running_sum += v |
| 42 | + |
| 43 | + return running_sum |
| 44 | + |
| 45 | + |
| 46 | +class MapSumPrefix(object): |
| 47 | + """ |
| 48 | + We can remember the answer for all possible prefixes in a HashMap score. When we get a new (key, val) pair, we |
| 49 | + update every prefix of key appropriately: each prefix will be changed by delta = val - map[key], where map is the |
| 50 | + previously associated value of key (zero if undefined.) |
| 51 | +
|
| 52 | + Time Complexity: Every insert operation is O(K^2), where K is the length of the key, as K strings are made of an |
| 53 | + average length of K. Every sum operation is O(1). |
| 54 | +
|
| 55 | + Space Complexity: The space used by map is linear in the size of all input key and val values combined. |
| 56 | + """ |
| 57 | + |
| 58 | + def __init__(self): |
| 59 | + self.mapping: Dict[str, int] = {} |
| 60 | + self.score = Counter() |
| 61 | + |
| 62 | + def insert(self, key: str, val: int) -> None: |
| 63 | + """ |
| 64 | + Inserts the key with the given value into the hash table |
| 65 | + Args: |
| 66 | + key (str): key to insert |
| 67 | + val (int): value to insert |
| 68 | + """ |
| 69 | + delta = val - self.mapping.get(key, 0) |
| 70 | + self.mapping[key] = val |
| 71 | + for i in range(len(key) + 1): |
| 72 | + prefix = key[:i] |
| 73 | + self.score[prefix] += delta |
| 74 | + |
| 75 | + def sum(self, prefix: str) -> int: |
| 76 | + """ |
| 77 | + Finds the sum of all keys with the prefix `prefix`. |
| 78 | + Args: |
| 79 | + prefix (str): prefix to search for |
| 80 | + Returns: |
| 81 | + int: sum of all keys with the prefix `prefix` |
| 82 | + """ |
| 83 | + return self.score[prefix] |
| 84 | + |
| 85 | + |
| 86 | +class MapSumTrie(object): |
| 87 | + """ |
| 88 | + Since we are dealing with prefixes, a Trie (prefix tree) is a natural data structure to approach this problem. For |
| 89 | + every node of the trie corresponding to some prefix, we will remember the desired answer (score) and store it at |
| 90 | + this node. As in the approach of using a prefix has map, this involves modifying each node by delta = val - map[key]. |
| 91 | +
|
| 92 | + Time Complexity: Every insert operation is O(K), where K is the length of the key. Every sum operation is O(K). |
| 93 | + Space Complexity: The space used is linear in the size of the total input. |
| 94 | + """ |
| 95 | + |
| 96 | + def __init__(self): |
| 97 | + self.mapping: Dict[str, int] = {} |
| 98 | + self.score = Counter() |
| 99 | + self.root = TrieNode() |
| 100 | + |
| 101 | + def insert(self, key: str, val: int) -> None: |
| 102 | + """ |
| 103 | + Inserts the key with the given value into the hash table |
| 104 | + Args: |
| 105 | + key (str): key to insert |
| 106 | + val (int): value to insert |
| 107 | + """ |
| 108 | + delta = val - self.mapping.get(key, 0) |
| 109 | + self.mapping[key] = val |
| 110 | + current = self.root |
| 111 | + current.score += delta |
| 112 | + for char in key: |
| 113 | + current = current.children[char] |
| 114 | + current.score += delta |
| 115 | + |
| 116 | + def sum(self, prefix: str) -> int: |
| 117 | + """ |
| 118 | + Finds the sum of all keys with the prefix `prefix`. |
| 119 | + Args: |
| 120 | + prefix (str): prefix to search for |
| 121 | + Returns: |
| 122 | + int: sum of all keys with the prefix `prefix` |
| 123 | + """ |
| 124 | + current = self.root |
| 125 | + for char in prefix: |
| 126 | + if char not in current.children: |
| 127 | + return 0 |
| 128 | + current = current.children[char] |
| 129 | + return current.score |
0 commit comments