Design Authentication Manager LeetCode solution in C++

1
class AuthenticationManager {
2
int ttl;
3
unordered_map<string, int> tokens;
4

5
public:
6
AuthenticationManager(int timeToLive) {
7
ttl = timeToLive;
8
}
9

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

14
void renew(string tokenId, int currentTime) {
15
auto tokenIt = tokens.find(tokenId);
16
if (tokenIt != end(tokens) && tokenIt->second > currentTime) {
17
tokenIt->second = currentTime + ttl;
18
}
19
}
20

21
int countUnexpiredTokens(int currentTime) {
22
int res = 0;
23
for (auto token : tokens) {
24
if (token.second > currentTime) res++;
25
}
26
return res;
27
}
28
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0