🔥Easy Solutions in Java 📝, Python 🐍, and C++ 🖥️🧐Look at once 💻
Intuition\n Describe your first thoughts on how to solve this problem. \nWe need to maintain the history of visited URLs so that we can move back and forward. W
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to maintain the history of visited URLs so that we can move back and forward. We can use a stack to maintain the history of visited URLs.\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- We can maintain two stacks, stackBack and stackForward, to represent the back and forward history respectively.\n- When we visit a new URL, we push it onto the stackBack and clear the stackForward since we can no longer move forward after visiting a new URL.\n- When we move back, we pop the top element from stackBack and push it onto stackForward.\n- When we move forward, we pop the top element from stackForward and push it onto stackBack.\n\n\n\n\n\n# Complexity\n1. Time complexity:\n- visit method takes O(1) time complexity since we only push the URL onto the stackBack.\n- back method takes O(min(steps, size(stackBack))) time complexity since we need to pop the top elements from stackBack and push them onto stackForward at most steps times.\n- forward method takes O(min(steps, size(stackForward))) time complexity since we need to pop the top elements from stackForward and push them onto stackBack at most steps times.\n\n\n2. Space complexity:\n - O(n) where n is the total number of visited URLs. We need to store the visited URLs in stackBack and stackForward.\n\n\n\n\n\n![image.png](https://assets.leetcode.com/users/images/b427e686-2e5d-469a-8e7a-db5140022a6b_1677715904.0948765.png)\n\n\n# Please Upvote\uD83D\uDC4D\uD83D\uDC4D\n```\nThanks for visiting my solution.\uD83D\uDE0A Keep Learning\nPlease give my solution an upvote! \uD83D\uDC4D\nIt\'s a simple way to show your appreciation and\nkeep me motivated. Thank you! \uD83D\uDE0A\n```\n# Code\n``` Java []\nimport java.util.*;\n\nclass BrowserHistory {\n\n    private Deque<String> history;\n    private Deque<String> forward;\n\n    public BrowserHistory(String homepage) {\n        history = new LinkedList<>();\n        history.push(homepage);\n        forward = new LinkedList<>();\n    }\n    \n    public void visit(String url) {\n        history.push(url);\n        forward.clear();\n    }\n    \n    public String back(int steps) {\n        while (steps > 0 && history.size() > 1) {\n            forward.push(history.pop());\n            steps--;\n        }\n        return history.peek();\n    }\n    \n    public String forward(int steps) {\n        while (steps > 0 && !forward.isEmpty()) {\n            history.push(forward.pop());\n            steps--;\n        }\n        return history.peek();\n    }\n}\n\n/**\n * Your BrowserHistory object will be instantiated and called as such:\n * BrowserHistory obj = new BrowserHistory(homepage);\n * obj.visit(url);\n * String param_2 = obj.back(steps);\n * String param_3 = obj.forward(steps);\n */\n\n\n```\n```Python []\nclass BrowserHistory(object):\n\n    def __init__(self, homepage):\n        """\n        :type homepage: str\n        """\n        self.history = [homepage]\n        self.current_index = 0\n\n    def visit(self, url):\n        """\n        :type url: str\n        :rtype: None\n        """\n        # Clear forward history\n        self.history = self.history[:self.current_index+1]\n        # Add new url to history\n        self.history.append(url)\n        # Update current index\n        self.current_index = len(self.history) - 1\n\n    def back(self, steps):\n        """\n        :type steps: int\n        :rtype: str\n        """\n        # Calculate new index after moving back steps\n        self.current_index = max(0, self.current_index - steps)\n        return self.history[self.current_index]\n\n    def forward(self, steps):\n        """\n        :type steps: int\n        :rtype: str\n        """\n        # Calculate new index after moving forward steps\n        self.current_index = min(len(self.history) - 1, self.current_index + steps)\n        return self.history[self.current_index]\n\n\n# Your BrowserHistory object will be instantiated and called as such:\n# obj = BrowserHistory(homepage)\n# obj.visit(url)\n# param_2 = obj.back(steps)\n# param_3 = obj.forward(steps)\n\n\n```\n```C++ []\n\nclass BrowserHistory {\npublic:\n    BrowserHistory(string homepage) {\n        history.push_back(homepage);\n        cur_idx = 0;\n    }\n    \n    void visit(string url) {\n        // Clear forward history\n        history.resize(cur_idx + 1);\n        history.push_back(url);\n        cur_idx++;\n    }\n    \n    string back(int steps) {\n        int prev_idx = max(0, cur_idx - steps);\n        cur_idx = prev_idx;\n        return history[cur_idx];\n    }\n    \n    string forward(int steps) {\n        int next_idx = min((int)history.size() - 1, cur_idx + steps);\n        cur_idx = next_idx;\n        return history[cur_idx];\n    }\n\nprivate:\n    vector<string> history;\n    int cur_idx;\n};\n\n\n\n```\n# Please Comment\uD83D\uDC4D\uD83D\uDC4D\n```\nThanks for visiting my solution comment below if you like it.\uD83D\uDE0A\n```
['Linked List', 'Queue', 'Python', 'C++', 'Java']