"content":"<p>There is an authentication system that works with authentication tokens. For each session, the user will receive a new authentication token that will expire <code>timeToLive</code> seconds after the <code>currentTime</code>. If the token is renewed, the expiry time will be <b>extended</b> to expire <code>timeToLive</code> seconds after the (potentially different) <code>currentTime</code>.</p>\n\n<p>Implement the <code>AuthenticationManager</code> class:</p>\n\n<ul>\n\t<li><code>AuthenticationManager(int timeToLive)</code> constructs the <code>AuthenticationManager</code> and sets the <code>timeToLive</code>.</li>\n\t<li><code>generate(string tokenId, int currentTime)</code> generates a new token with the given <code>tokenId</code> at the given <code>currentTime</code> in seconds.</li>\n\t<li><code>renew(string tokenId, int currentTime)</code> renews the <strong>unexpired</strong> token with the given <code>tokenId</code> at the given <code>currentTime</code> in seconds. If there are no unexpired tokens with the given <code>tokenId</code>, the request is ignored, and nothing happens.</li>\n\t<li><code>countUnexpiredTokens(int currentTime)</code> returns the number of <strong>unexpired</strong> tokens at the given currentTime.</li>\n</ul>\n\n<p>Note that if a token expires at time <code>t</code>, and another action happens on time <code>t</code> (<code>renew</code> or <code>countUnexpiredTokens</code>), the expiration takes place <strong>before</strong> the other actions.</p>\n\n<p> </p>\n<p><strong>Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/02/25/copy-of-pc68_q2.png\" style=\"width: 500px; height: 287px;\"/>\n<pre>\n<strong>Input</strong>\n["AuthenticationManager","<code>renew</code>","generate","<code>countUnexpiredTokens</code>","generate","<code>renew</code>","<code>renew</code>","<code>countUnexpiredTokens</code>"]\n[[5],["aaa",1],["aaa",2],[6],["bbb",7],["aaa",8],["bbb",10],[15]]\n<strong>Output</strong>\n[null,null,null,1,null,null,null,0]\n\n<strong>Explanation</strong>\nAuthenticationManagerauthenticationManager=newAuthenticationManager(5);// Constructs the AuthenticationManager with <code>timeToLive</code> = 5 seconds.\nauthenticationManager.<code>renew</code>("aaa", 1); // No token exists with tokenId "aaa" at time 1, so nothing happens.\nauthenticationManager.generate("aaa", 2); // Generates a new token with tokenId "aaa" at time 2.\nauthenticationManager.<code>countUnexpiredTokens</code>(6); // The token with tokenId "aaa" is the only unexpired one at time 6, so return 1.\nauthenticationManager.generate("bbb", 7); // Generates a new token with tokenId "bbb" at time 7.\nauthenticationManager.<code>renew</code>("aaa", 8); // The token with tokenId "aaa" expired at time 7, and 8 >= 7, so at time 8 the <code>renew</code> request is ignored, and nothing happens.\nauthenticationManager.<code>renew</code>("bbb", 10); // The token with tokenId "bbb" is unexpired at time 10, so the <code>renew</code> request is fulfilled and now the token will expire at time 15.\nauthenticationManager.<code>countUnexpiredTokens</code>(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.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 <= timeToLive <= 10<sup>8</sup></code></li>\n\t<li><code>1 <= currentTime <= 10<sup>8</sup></code></li>\n\t<li><code>1 <= tokenId.length <= 5</code></li>\n\t<li><code>tokenId</code> consists only of lowercase letters.</li>\n\t<li>All calls to <code>generate</code> will contain unique values of <code>tokenId</code>.</li>\n\t<li>The values of <code>currentTime</code> across all the func
"code":"class AuthenticationManager {\npublic:\n AuthenticationManager(int timeToLive) {\n\n }\n \n void generate(string tokenId, int currentTime) {\n\n }\n \n void renew(string tokenId, int currentTime) {\n\n }\n \n int countUnexpiredTokens(int currentTime) {\n\n }\n};\n\n/**\n * Your AuthenticationManager object will be instantiated and called as such:\n * AuthenticationManager* obj = new AuthenticationManager(timeToLive);\n * obj->generate(tokenId,currentTime);\n * obj->renew(tokenId,currentTime);\n * int param_3 = obj->countUnexpiredTokens(currentTime);\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"Java",
"langSlug":"java",
"code":"class AuthenticationManager {\n\n public AuthenticationManager(int timeToLive) {\n\n }\n \n public void generate(String tokenId, int currentTime) {\n\n }\n \n public void renew(String tokenId, int currentTime) {\n\n }\n \n public int countUnexpiredTokens(int currentTime) {\n\n }\n}\n\n/**\n * Your AuthenticationManager object will be instantiated and called as such:\n * AuthenticationManager obj = new AuthenticationManager(timeToLive);\n * obj.generate(tokenId,currentTime);\n * obj.renew(tokenId,currentTime);\n * int param_3 = obj.countUnexpiredTokens(currentTime);\n */",
"code":"class AuthenticationManager:\n\n def __init__(self, timeToLive: int):\n\n\n def generate(self, tokenId: str, currentTime: int) -> None:\n\n\n def renew(self, tokenId: str, currentTime: int) -> None:\n\n\n def countUnexpiredTokens(self, currentTime: int) -> int:\n\n\n\n# Your AuthenticationManager object will be instantiated and called as such:\n# obj = AuthenticationManager(timeToLive)\n# obj.generate(tokenId,currentTime)\n# obj.renew(tokenId,currentTime)\n# param_3 = obj.countUnexpiredTokens(currentTime)",
"__typename":"CodeSnippetNode"
},
{
"lang":"C",
"langSlug":"c",
"code":"\n\n\ntypedef struct {\n \n} AuthenticationManager;\n\n\nAuthenticationManager* authenticationManagerCreate(int timeToLive) {\n \n}\n\nvoid authenticationManagerGenerate(AuthenticationManager* obj, char * tokenId, int currentTime) {\n \n}\n\nvoid authenticationManagerRenew(AuthenticationManager* obj, char * tokenId, int currentTime) {\n \n}\n\nint authenticationManagerCountUnexpiredTokens(AuthenticationManager* obj, int currentTime) {\n \n}\n\nvoid authenticationManagerFree(AuthenticationManager* obj) {\n \n}\n\n/**\n * Your AuthenticationManager struct will be instantiated and called as such:\n * AuthenticationManager* obj = authenticationManagerCreate(timeToLive);\n * authenticationManagerGenerate(obj, tokenId, currentTime);\n \n * authenticationManagerRenew(obj, tokenId, currentTime);\n \n * int param_3 = authenticationManagerCountUnexpiredTokens(obj, currentTime);\n \n * authenticationManagerFree(obj);\n*/",
"__typename":"CodeSnippetNode"
},
{
"lang":"C#",
"langSlug":"csharp",
"code":"public class AuthenticationManager {\n\n public AuthenticationManager(int timeToLive) {\n\n }\n \n public void Generate(string tokenId, int currentTime) {\n\n }\n \n public void Renew(string tokenId, int currentTime) {\n\n }\n \n public int CountUnexpiredTokens(int currentTime) {\n\n }\n}\n\n/**\n * Your AuthenticationManager object will be instantiated and called as such:\n * AuthenticationManager obj = new AuthenticationManager(timeToLive);\n * obj.Generate(tokenId,currentTime);\n * obj.Renew(tokenId,currentTime);\n * int param_3 = obj.CountUnexpiredTokens(currentTime);\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"JavaScript",
"langSlug":"javascript",
"code":"/**\n * @param {number} timeToLive\n */\nvar AuthenticationManager = function(timeToLive) {\n\n};\n\n/** \n * @param {string} tokenId \n * @param {number} currentTime\n * @return {void}\n */\nAuthenticationManager.prototype.generate = function(tokenId, currentTime) {\n\n};\n\n/** \n * @param {string} tokenId \n * @param {number} currentTime\n * @return {void}\n */\nAuthenticationManager.prototype.renew = function(tokenId, currentTime) {\n\n};\n\n/** \n * @param {number} currentTime\n * @return {number}\n */\nAuthenticationManager.prototype.countUnexpiredTokens = function(currentTime) {\n\n};\n\n/**\n * Your AuthenticationManager object will be instantiated and called as such:\n * var obj = new AuthenticationManager(timeToLive)\n * obj.generate(tokenId,currentTime)\n * obj.renew(tokenId,currentTime)\n * var param_3 = obj.countUnexpiredTokens(currentTime)\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"Ruby",
"langSlug":"ruby",
"code":"class AuthenticationManager\n\n=begin\n :type time_to_live: Integer\n=end\n def initialize(time_to_live)\n\n end\n\n\n=begin\n :type token_id: String\n :type current_time: Integer\n :rtype: Void\n=end\n def generate(token_id, current_time)\n\n end\n\n\n=begin\n :type token_id: String\n :type current_time: Integer\n :rtype: Void\n=end\n def renew(token_id, current_time)\n\n end\n\n\n=begin\n :type current_time: Integer\n :rtype: Integer\n=end\n def count_unexpired_tokens(current_time)\n\n end\n\n\nend\n\n# Your AuthenticationManager object will be instantiated and called as such:\n# obj = AuthenticationManager.new(time_to_live)\n# obj.generate(token_id, current_time)\n# obj.renew(token_id, current_time)\n# param_3 = obj.count_unexpired_tokens(current_time)",
"__typename":"CodeSnippetNode"
},
{
"lang":"Swift",
"langSlug":"swift",
"code":"\nclass AuthenticationManager {\n\n init(_ timeToLive: Int) {\n\n }\n \n func generate(_ tokenId: String, _ currentTime: Int) {\n\n }\n \n func renew(_ tokenId: String, _ currentTime: Int) {\n\n }\n \n func countUnexpiredTokens(_ currentTime: Int) -> Int {\n\n }\n}\n\n/**\n * Your AuthenticationManager object will be instantiated and called as such:\n * let obj = AuthenticationManager(timeToLive)\n * obj.generate(tokenId, currentTime)\n * obj.renew(tokenId, currentTime)\n * let ret_3: Int = obj.countUnexpiredTokens(currentTime)\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"Go",
"langSlug":"golang",
"code":"type AuthenticationManager struct {\n\n}\n\n\nfunc Constructor(timeToLive int) AuthenticationManager {\n\n}\n\n\nfunc (this *AuthenticationManager) Generate(tokenId string, currentTime int) {\n\n}\n\n\nfunc (this *AuthenticationManager) Renew(tokenId string, currentTime int) {\n\n}\n\n\nfunc (this *AuthenticationManager) CountUnexpiredTokens(currentTime int) int {\n\n}\n\n\n/**\n * Your AuthenticationManager object will be instantiated and called as such:\n * obj := Constructor(timeToLive);\n * obj.Generate(tokenId,currentTime);\n * obj.Renew(tokenId,currentTime);\n * param_3 := obj.CountUnexpiredTokens(currentTime);\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"Scala",
"langSlug":"scala",
"code":"class AuthenticationManager(_timeToLive: Int) {\n\n def generate(tokenId: String, currentTime: Int) {\n \n }\n\n def renew(tokenId: String, currentTime: Int) {\n \n }\n\n def countUnexpiredTokens(currentTime: Int): Int = {\n \n }\n\n}\n\n/**\n * Your AuthenticationManager object will be instantiated and called as such:\n * var obj = new AuthenticationManager(timeToLive)\n * obj.generate(tokenId,currentTime)\n * obj.renew(tokenId,currentTime)\n * var param_3 = obj.countUnexpiredTokens(currentTime)\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"Kotlin",
"langSlug":"kotlin",
"code":"class AuthenticationManager(timeToLive: Int) {\n\n fun generate(tokenId: String, currentTime: Int) {\n\n }\n\n fun renew(tokenId: String, currentTime: Int) {\n\n }\n\n fun countUnexpiredTokens(currentTime: Int): Int {\n\n }\n\n}\n\n/**\n * Your AuthenticationManager object will be instantiated and called as such:\n * var obj = AuthenticationManager(timeToLive)\n * obj.generate(tokenId,currentTime)\n * obj.renew(tokenId,currentTime)\n * var param_3 = obj.countUnexpiredTokens(currentTime)\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"Rust",
"langSlug":"rust",
"code":"struct AuthenticationManager {\n\n}\n\n\n/**\n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl AuthenticationManager {\n\n fn new(timeToLive: i32) -> Self {\n\n }\n \n fn generate(&self, token_id: String, current_time: i32) {\n\n }\n \n fn renew(&self, token_id: String, current_time: i32) {\n\n }\n \n fn count_unexpired_tokens(&self, current_time: i32) -> i32 {\n\n }\n}\n\n/**\n * Your AuthenticationManager object will be instantiated and called as such:\n * let obj = AuthenticationManager::new(timeToLive);\n * obj.generate(tokenId, currentTime);\n * obj.renew(tokenId, currentTime);\n * let ret_3: i32 = obj.count_unexpired_tokens(currentTime);\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"PHP",
"langSlug":"php",
"code":"class AuthenticationManager {\n /**\n * @param Integer $timeToLive\n */\n function __construct($timeToLive) {\n\n }\n\n /**\n * @param String $tokenId\n * @param Integer $currentTime\n * @return NULL\n */\n function generate($tokenId, $currentTime) {\n\n }\n\n /**\n * @param String $tokenId\n * @param Integer $currentTime\n * @return NULL\n */\n function renew($tokenId, $currentTime) {\n\n }\n\n /**\n * @param Integer $currentTime\n * @return Integer\n */\n function countUnexpiredTokens($currentTime) {\n\n }\n}\n\n/**\n * Your AuthenticationManager object will be instantiated and called as such:\n * $obj = AuthenticationManager($timeToLive);\n * $obj->generate($tokenId, $currentTime);\n * $obj->renew($tokenId, $currentTime);\n * $ret_3 = $obj->countUnexpiredTokens($currentTime);\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"TypeScript",
"langSlug":"typescript",
"code":"class AuthenticationManager {\n constructor(timeToLive: number) {\n\n }\n\n generate(tokenId: string, currentTime: number): void {\n\n }\n\n renew(tokenId: string, currentTime: number): void {\n\n }\n\n countUnexpiredTokens(currentTime: number): number {\n\n }\n}\n\n/**\n * Your AuthenticationManager object will be instantiated and called as such:\n * var obj = new AuthenticationManager(timeToLive)\n * obj.generate(tokenId,currentTime)\n * obj.renew(tokenId,currentTime)\n * var param_3 = obj.countUnexpiredTokens(currentTime)\n */",
"code":"-spec authentication_manager_init_(TimeToLive :: integer()) -> any().\nauthentication_manager_init_(TimeToLive) ->\n .\n\n-spec authentication_manager_generate(TokenId :: unicode:unicode_binary(), CurrentTime :: integer()) -> any().\nauthentication_manager_generate(TokenId, CurrentTime) ->\n .\n\n-spec authentication_manager_renew(TokenId :: unicode:unicode_binary(), CurrentTime :: integer()) -> any().\nauthentication_manager_renew(TokenId, CurrentTime) ->\n .\n\n-spec authentication_manager_count_unexpired_tokens(CurrentTime :: integer()) -> integer().\nauthentication_manager_count_unexpired_tokens(CurrentTime) ->\n .\n\n\n%% Your functions will be called as such:\n%% authentication_manager_init_(TimeToLive),\n%% authentication_manager_generate(TokenId, CurrentTime),\n%% authentication_manager_renew(TokenId, CurrentTime),\n%% Param_3 = authentication_manager_count_unexpired_tokens(CurrentTime),\n\n%% authentication_manager_init_ will be called before every test case, in which you can do some necessary initializations.",
"__typename":"CodeSnippetNode"
},
{
"lang":"Elixir",
"langSlug":"elixir",
"code":"defmodule AuthenticationManager do\n @spec init_(time_to_live :: integer) :: any\n def init_(time_to_live) do\n\n end\n\n @spec generate(token_id :: String.t, current_time :: integer) :: any\n def generate(token_id, current_time) do\n\n end\n\n @spec renew(token_id :: String.t, current_time :: integer) :: any\n def renew(token_id, current_time) do\n\n end\n\n @spec count_unexpired_tokens(current_time :: integer) :: integer\n def count_unexpired_tokens(current_time) do\n\n end\nend\n\n# Your functions will be called as such:\n# AuthenticationManager.init_(time_to_live)\n# AuthenticationManager.generate(token_id, current_time)\n# AuthenticationManager.renew(token_id, current_time)\n# param_3 = AuthenticationManager.count_unexpired_tokens(current_time)\n\n# AuthenticationManager.init_ will be called before every test case, in which you can do some necessary initializations.",