Formatted question description: https://leetcode.ca/all/1797.html

Algorithm

Design a hash table to store the token and expiration time of each verification code.

class AuthenticationManager {
    int timeToLive;
    Map<String, Integer> map;

    public AuthenticationManager(int timeToLive) {
        this.timeToLive = timeToLive;
        map = new HashMap<String, Integer>();
    }
    
    public void generate(String tokenId, int currentTime) {
        map.put(tokenId, currentTime + timeToLive);
    }
    
    public void renew(String tokenId, int currentTime) {
        clearMap(currentTime);
        if (map.get(tokenId) != null)
            map.put(tokenId, currentTime + timeToLive);
    }
    
    public int countUnexpiredTokens(int currentTime) {
        clearMap(currentTime);
        return map.size();
    }
    
    public void clearMap(int currentTime) {
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            if ((int)entry.getValue() <= currentTime)
                it.remove();
        }
    }
}

All Problems

All Solutions