Problem statement

https://leetcode.com/problems/logger-rate-limiter/

Solution

Just keep hash table self.d, where for each name we have corresponding timestamp. Function shouldPrintMessage will check if we already have message in hash table or not. If it is in the table, check how long ago it was.

Complexity

Time complexity is $O(1)$ for both initialization and shouldPrintMessage. Space complexity is $O(n)$, where $n$ is total number of different messages.

Code

class Logger:
    def __init__(self):
        self.d = {}
        
    def shouldPrintMessage(self, timestamp, message):
        if message not in self.d:
            self.d[message] = timestamp
            return True
        else:
            if self.d[message] + 10 > timestamp:
                return False
            else:
                self.d[message] = timestamp
                return True