Leetcode 359 Solution

This article provides solution to leetcode question 359 (logger-rate-limiter)

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

Solution

class Logger {
    unordered_map<string, int> m;
public:
    /** Initialize your data structure here. */
    Logger() {

    }

    /** Returns true if the message should be printed in the given timestamp, otherwise returns false.
        If this method returns false, the message will not be printed.
        The timestamp is in seconds granularity. */
    bool shouldPrintMessage(int timestamp, string message) {
        if (m.find(message) == m.end() || timestamp - m[message] >= 10)
        {
            m[message] = timestamp;
            return true;
        }
        else
            return false;
    }
};

/**
 * Your Logger object will be instantiated and called as such:
 * Logger obj = new Logger();
 * bool param_1 = obj.shouldPrintMessage(timestamp,message);
 */