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", "back", "forward", "visit", "forward", "back", "back"]
args = [["start"], ["maps"], ["wiki"], [2], [1], ["shop"], [2], [1], [5]]
Output: [None, None, None, "start", "maps", None, "shop", "maps", "start"]
Explanation: after visiting "shop" from "maps", "wiki" is no longer reachable, so forward(2) stays on "shop";
back(5) stops at "start" because there is nothing older.
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