There is an authentication system that works with authentication tokens. For each session, the user will receive a new authentication token that will expire timeToLive seconds after the currentTime. If the token is renewed, the expiry time will be extended to expire timeToLive seconds after the (potentially different) currentTime.

Implement the AuthenticationManager class:


	AuthenticationManager(int timeToLive) constructs the AuthenticationManager and sets the timeToLive.
	generate(string tokenId, int currentTime) generates a new token with the given tokenId at the given currentTime in seconds.
	renew(string tokenId, int currentTime) renews the unexpired token with the given tokenId at the given currentTime in seconds. If there are no unexpired tokens with the given tokenId, the request is ignored, and nothing happens.
	countUnexpiredTokens(int currentTime) returns the number of unexpired tokens at the given currentTime.


Note that if a token expires at time t, and another action happens on time t (renew or countUnexpiredTokens), the expiration takes place before the other actions.

&nbsp;
Example 1:

Input
["AuthenticationManager", "renew", "generate", "countUnexpiredTokens", "generate", "renew", "renew", "countUnexpiredTokens"]
[[5], ["aaa", 1], ["aaa", 2], [6], ["bbb", 7], ["aaa", 8], ["bbb", 10], [15]]
Output
[null, null, null, 1, null, null, null, 0]

Explanation
AuthenticationManager authenticationManager = new AuthenticationManager(5); // Constructs the AuthenticationManager with timeToLive = 5 seconds.
authenticationManager.renew("aaa", 1); // No token exists with tokenId "aaa" at time 1, so nothing happens.
authenticationManager.generate("aaa", 2); // Generates a new token with tokenId "aaa" at time 2.
authenticationManager.countUnexpiredTokens(6); // The token with tokenId "aaa" is the only unexpired one at time 6, so return 1.
authenticationManager.generate("bbb", 7); // Generates a new token with tokenId "bbb" at time 7.
authenticationManager.renew("aaa", 8); // The token with tokenId "aaa" expired at time 7, and 8 &gt;= 7, so at time 8 the renew request is ignored, and nothing happens.
authenticationManager.renew("bbb", 10); // The token with tokenId "bbb" is unexpired at time 10, so the renew request is fulfilled and now the token will expire at time 15.
authenticationManager.countUnexpiredTokens(15); // The token with tokenId "bbb" expires at time 15, and the token with tokenId "aaa" expired at time 7, so currently no token is unexpired, so return 0.


&nbsp;
Constraints:


	1 &lt;= timeToLive &lt;= 108
	1 &lt;= currentTime &lt;= 108
	1 &lt;= tokenId.length &lt;= 5
	tokenId consists only of lowercase letters.
	All calls to generate will contain unique values of tokenId.
	The values of currentTime across all the function calls will be strictly increasing.
	At most 2000 calls will be made to all functions combined.


class AuthenticationManager(object):

    def __init__(self, timeToLive):
        self.token = dict()
        self.time = timeToLive # store timeToLive and create dictionary

    def generate(self, tokenId, currentTime):
        self.token[tokenId] = currentTime # store tokenId with currentTime

    def renew(self, tokenId, currentTime):
        limit = currentTime-self.time # calculate limit time to filter unexpired tokens
        if tokenId in self.token and self.token[tokenId]>limit: # filter tokens and renew its time
            self.token[tokenId] = currentTime

    def countUnexpiredTokens(self, currentTime):
        limit = currentTime-self.time # calculate limit time to filter unexpired tokens
        c = 0
        for i in self.token:
            if self.token[i]>limit: # count unexpired tokens
                c+=1
        return c
class AuthenticationManager {
    private int ttl;
    private Map<String, Integer> map;

    public AuthenticationManager(int timeToLive) {
        this.ttl = timeToLive;
        this.map = new HashMap<>();
    }
    
    public void generate(String tokenId, int currentTime) {
        map.put(tokenId, currentTime + this.ttl);
    }
    
    public void renew(String tokenId, int currentTime) {
        Integer expirationTime = this.map.getOrDefault(tokenId, null);
        if (expirationTime == null || expirationTime <= currentTime)
            return;
        
        generate(tokenId, currentTime);
    }
    
    public int countUnexpiredTokens(int currentTime) {
        int count = 0;
        for (Map.Entry<String, Integer> entry: this.map.entrySet())
            if (entry.getValue() > currentTime)
                count++;
        
        return count;
    }
}
class AuthenticationManager {
    int ttl;
    unordered_map<string, int> tokens;
public:
    AuthenticationManager(int timeToLive) {
        ttl = timeToLive;
    }

    void generate(string tokenId, int currentTime) {
        tokens[tokenId] = currentTime + ttl;
    }

    void renew(string tokenId, int currentTime) {
        auto tokenIt = tokens.find(tokenId);
        if (tokenIt != end(tokens) && tokenIt->second > currentTime) {
            tokenIt->second = currentTime + ttl;
        }
    }

    int countUnexpiredTokens(int currentTime) {
        int res = 0;
        for (auto token: tokens) {
            if (token.second > currentTime) res++;
        }
        return res;
    }
};
// O(n)
var AuthenticationManager = function(timeToLive) {
    this.ttl = timeToLive;
    this.map = {};
};
AuthenticationManager.prototype.generate = function(tokenId, currentTime) {
    this.map[tokenId] = currentTime + this.ttl;
};
AuthenticationManager.prototype.renew = function(tokenId, currentTime) {
    let curr = this.map[tokenId];
    if (curr > currentTime) {
        this.generate(tokenId, currentTime);
    }
};
AuthenticationManager.prototype.countUnexpiredTokens = function(currentTime) {
    return Object.keys(this.map).filter(key => this.map[key] > currentTime).length;
};
Design Authentication Manager