Design a class TabHistory that models the back and forward buttons of one browser tab.
TabHistory(homepage): open the tab onhomepage.visit(url): navigate tourlfrom the current page. This discards all forward history.back(steps): go back up tostepspages (stop at the oldest page if fewer are available) and return the current page.forward(steps): go forward up tostepspages (stop at the newest page if fewer are available) and return the current page.
Examples
Input: ops = ["TabHistory", "visit", "visit", "visit", "back", "back", "forward", "visit", "forward", "back", "back"]
args = [["home"], ["news"], ["mail"], ["docs"], [1], [1], [1], ["chat"], [2], [2], [7]]
Output: [None, None, None, None, "mail", "news", "mail", None, "chat", "news", "home"]
Explanation: after visiting "chat" from "mail", "docs" is no longer reachable; forward(2) stays on "chat".
Constraints
- At most
5000operations 1 <= steps <= 100- Target:
O(steps)per call
Goals
- Keep back and forward pages on two stacks
- Clear the forward stack when a new page is visited
- Clamp step counts that exceed the available history