Return Records 🥝
Problem statement
Simulate a website authentication service. Initially, no users are registered and no user is logged in. Process each request in attempts from left to right.
Each request has one of these forms:
register username password: register a new username. Return"Registered Successfully", or"Register Unsuccessfully"if the username already exists.login username password: log in a registered user when the password matches and the user is not already logged in. Return"Logged In Successfully"on success, or"Login Unsuccessfully"otherwise.logout username: log out a currently logged-in user. Return"Logged Out Successfully"on success, or"Logout Unsuccessfully"otherwise.
A failed login does not change an existing session. Usernames and passwords are case-sensitive.
Return one result string for every request, in the same order.
Function
returnRecords(attempts: String[]) → String[]Examples
Example 1
attempts = ["register user05 qwerty", "login user05 qwerty", "logout user05"]return = ["Registered Successfully", "Logged In Successfully", "Logged Out Successfully"]The user is registered, then logs in with the matching password, and finally logs out. All three requests succeed.
Example 2
attempts = ["register david david123", "register adam 1Adam1", "login david david123", "login adam 1adam1", "logout david"]return = ["Registered Successfully", "Registered Successfully", "Logged In Successfully", "Login Unsuccessfully", "Logged Out Successfully"]davidandadamare new usernames, so both registrations succeed.davidlogs in with the correct password.adamuses1adam1, which does not match the case-sensitive password1Adam1, so that login fails.davidis logged in, so the logout succeeds.
Constraints
1 <= attempts.length <= 1000001 <= username.length, password.length <= 10- Usernames and passwords contain only digits and English letters.
- Every request has one of the three documented formats.