Browser History Tabs
Medium · design
A browser has any number of tabs. Each tab has a name and its own history of pages, with one page currently shown. You are given a list of operations. operations[i] is one of the names below, and args[i] is a list of strings holding its arguments:
"open"with[tab, url]: a new tab calledtabappears, showingurl. Its history is just that one page."visit"with[tab, url]: the tab goes tourl. Every page that was ahead of the current page in that tab's history is thrown away, andurlbecomes the current page."back"with[tab]: the tab steps one page back in its history. If it is already at its first page, nothing happens."forward"with[tab]: the tab steps one page forward in its history. If it is already at its last page, nothing happens."close"with[tab]: the tab is closed."reopen"with[]: the tab that was closed most recently, and has not been reopened since, comes back under its old name. Its history and its current page are exactly what they were when it was closed. If there is no such tab, nothing happens.
Every operation produces one result, a string:
"open","visit","back"and"forward": the page the tab shows afterwards."close": the page the tab was showing when it was closed."reopen": the name of the tab that came back, or the empty string""if there was none.
A name is given to a tab by only one "open" and never reused, and every "visit", "back", "forward" and "close" names a tab that is currently open. Return the list of results, in order.
Examples
Example 1
Input: operations = ["open", "visit", "visit", "back", "back", "back", "forward", "visit"], args = [["t1", "home"], ["t1", "news"], ["t1", "mail"], ["t1"], ["t1"], ["t1"], ["t1"], ["t1", "maps"]]
Output: ["home", "news", "mail", "news", "home", "home", "news", "maps"]
The third back does nothing because the tab is already at home. The last visit throws away mail, which was ahead of news.
Example 2
Input: operations = ["open", "open", "close", "visit", "reopen", "back", "reopen"], args = [["a", "one"], ["b", "two"], ["b"], ["a", "three"], [], ["b"], []]
Output: ["one", "two", "two", "three", "b", "two", ""]
Tab b comes back with its single page. The second reopen finds nothing left to bring back.
Example 3
Input: operations = ["open", "open", "visit", "close", "close", "reopen", "reopen", "back"], args = [["a", "p"], ["b", "q"], ["b", "r"], ["a"], ["b"], [], [], ["b"]]
Output: ["p", "q", "r", "p", "r", "b", "a", "q"]
Tab b was closed last, so it comes back first, then a. Tab b still has q behind r.
Constraints
- 1 <= len(operations) <= 10^4
- len(args) == len(operations)
- operations[i] is open, visit, back, forward, close or reopen
- Tab names and URLs are non-empty strings of 1 to 20 lowercase letters and digits
- Each tab name is given by exactly one open, and tabs named by other operations exist and are open