___|  _ \   |  |    |   |_ _|\ \     / ____|
 |     |   |  |  |    |   |  |  \ \   /  __|
 |   | |   | ___ __|  ___ |  |   \ \ /   |
\____|\___/     _|   _|  _|___|   \_/   _____| 

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

[LeetCode] 677. Map Sum Pairs (Python3)

BY: @juneepa | CREATED: March 3, 2018, 7:45 p.m. | VOTES: 1 | PAYOUT: $0.00 | [ VOTE ]

https://leetcode.com/problems/map-sum-pairs/description/

Implement a MapSum class with insert, and sum methods.

Check the website above for more details

Medium, 44ms, 100%

class MapSum:

## Initialize the dictionary
def __init__(self):
    self.wdict = {}

## Insert keys and value
## Since values will be overrided for the already existing key, just one line below will be enough
def insert(self, key, val):
    self.wdict[key] = val

## if the first len(prefix) letters equals to the prefix, add the key to the sum
def sum(self, prefix):
    sum = 0
    for key in self.wdict:
        if key[:len(prefix)] == prefix:
            sum += self.wdict[key]
    return sum

## Without Comments
class MapSum:

def __init__(self):
    self.wdict = {}

def insert(self, key, val):
    self.wdict[key] = val

def sum(self, prefix):
    sum = 0
    for key in self.wdict:
        if key[:len(prefix)] == prefix:
            sum += self.wdict[key]
    return sum
TAGS: [ #kr ] [ #python ] [ #leetcode ] [ #algorithm ] [ #programming ]

Replies

NO REPLIES FOUND.

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>