Clean Codes🔥🔥|| Full Explanation✅|| Stack Method✅|| C++|| Java|| Python3
What Problem says ?\n- Create a class called BrowserHistory that keeps track of the web pages a user has visited in a web browser. \n- Also make a visit method 
# What Problem says ?\n- Create a class called `BrowserHistory` that keeps track of the web pages a user has visited in a web browser. \n- Also make a `visit` method to add new pages to the `history` and `back` and `forward` methods to move the user back and forward in their browsing history. \n\n# Approach : Using Stacks\n- Here we are using two stacks: \n    1. `history` to keep track of the pages visited in the past, and \n    2. `future` to keep track of the pages that the user has "visited" in the future (i.e., after clicking the back button). \n- When the user clicks the `back` button, the `back` method pops URLs from the `history` stack and pushes them onto the `future` stack until it reaches the desired page. \n- When the user clicks the `forward` button, the `forward` method pops URLs from the `future` stack and pushes them onto the `history` stack until it reaches the desired page.\n<!-- Describe your approach to solving the problem. -->\n\n# Let\'s See an Example :\n```\nBrowserHistory history = new BrowserHistory("https://www.google.com");\n\n// User visits a few pages\nhistory.visit("https://www.google.com/search?q=java");\nhistory.visit("https://www.wikipedia.org/");\nhistory.visit("https://www.amazon.com/");\n\n// User clicks back button once\nString previousPage = history.back(1);\nSystem.out.println("User is now on page: " + previousPage);\n\n// User clicks forward button twice\nString nextPage = history.forward(2);\nSystem.out.println("User is now on page: " + nextPage);\n\n```\n- In this example, we create a new `BrowserHistory` object with the starting page of `https://www.google.com.` The user then visits three more pages by calling the visit method with the URLs of the pages.\n- Afterwards, the user clicks the `back` button once by calling `back(1)`, which pops one page from the `history` stack and pushes it onto the `future` stack. \n- The `back` method returns the current page URL after the navigation is complete, which we store in the `previousPage` variable and print out to the console.\n- Finally, the user clicks the `forward` button twice by calling `forward(2)`, which pops two pages from the `future` stack and pushes them onto the `history` stack. \n- The `forward` method also returns the current page URL after the navigation is complete, which we store in the `nextPage` variable and print out to the console.\n# Complexity :\n- Time complexity : O(steps)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity : O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Please Upvote\uD83D\uDC4D\uD83D\uDC4D\n```\nThanks for visiting my solution.\uD83D\uDE0A\n```\n# Codes [C++ |Java |Python3] : With Comments\n```Java []\nclass BrowserHistory {\n  private Deque<String> history = new ArrayDeque<>(); // Stack to store past pages\n  private Deque<String> future; // Stack to store future pages\n\n  // Constructor that takes the starting page URL and adds it to the history\n  public BrowserHistory(String homepage) {\n    visit(homepage);\n  }\n\n  // Adds a new page to the history and clears the future stack\n  public void visit(String url) {\n    history.push(url);\n    future = new ArrayDeque<>();\n  }\n\n  // Moves the user back a certain number of pages in the history\n  public String back(int steps) {\n    while (history.size() > 1 && steps-- > 0)\n      future.push(history.pop()); // Move pages from history to future stack\n    return history.peek(); // Return the current page after navigation\n  }\n\n  // Moves the user forward a certain number of pages in the future\n  public String forward(int steps) {\n    while (!future.isEmpty() && steps-- > 0)\n      history.push(future.pop()); // Move pages from future to history stack\n    return history.peek(); // Return the current page after navigation\n  }\n}\n\n```\n```C++ []\nclass BrowserHistory {\n    private:\n    stack<string> history; // Stack to store past pages\n    stack<string> future; // Stack to store future pages\n public:\n  // Constructor that takes the starting page URL and adds it to the history\n  BrowserHistory(string homepage) {\n    visit(homepage);\n  }\n\n  // Adds a new page to the history and clears the future stack\n  void visit(string url) {\n    history.push(url);\n    future = stack<string>(); // Clear the future stack\n  }\n\n  // Moves the user back a certain number of pages in the history\n  string back(int steps) {\n    while (history.size() > 1 && steps-- > 0)\n      future.push(history.top()), history.pop(); // Move pages from history to future stack\n    return history.top(); // Return the current page after navigation\n  }\n\n  // Moves the user forward a certain number of pages in the future\n  string forward(int steps) {\n    while (!future.empty() && steps-- > 0)\n      history.push(future.top()), future.pop(); // Move pages from future to history stack\n    return history.top(); // Return the current page after navigation\n  }\n};\n```\n```Python []\nclass BrowserHistory:\n  def __init__(self, homepage: str):\n    self.history = [] # Create an empty list to store the browsing history\n    self.visit(homepage) # Add the starting URL to the history list\n\n  def visit(self, url: str) -> None:\n    self.history.append(url) # Add the new URL to the history list\n    self.future = [] # Create an empty list to store future pages\n\n  def back(self, steps: int) -> str:\n    while len(self.history) > 1 and steps > 0:\n      # Move pages from the history list to the future list\n      self.future.append(self.history.pop())\n      steps -= 1\n    # Return the current page after navigation\n    return self.history[-1]\n\n  def forward(self, steps: int) -> str:\n    while self.future and steps > 0:\n      # Move pages from the future list to the history list\n      self.history.append(self.future.pop())\n      steps -= 1\n    # Return the current page after navigation\n    return self.history[-1]\n\n```\n# Please Upvote\uD83D\uDC4D\uD83D\uDC4D\n![ezgif-3-22a360561c.gif](https://assets.leetcode.com/users/images/94c993ff-345a-40f1-a095-170c8692857b_1679107487.1779711.gif)\n
['Stack', 'Python', 'C++', 'Java', 'Python3']