Using Two Stacks
Intuition\nWhy use Stack ? \nStack is used when we used to maintain the history. Since here, we need to maintain both the back and forward history two stacks wi
# Intuition\nWhy use Stack ? \nStack is used when we used to maintain the history. Since here, we need to maintain both the back and forward history two stacks will be used.\n\n# Approach\n1. We will keep on pushing urls in the back stack whenever visit function is called.\n2. We will keep a string variable which will keep track of our answer.\n3. In the back function, we will push the curr element into stack and pop the back array.\n\n# Complexity\n- Time complexity:\n1. for back function: O(no of steps)\n2. for forward function: O(no of steps)\n3. for visit function: O(n)\n\n# Code\n```java []\nclass BrowserHistory {\n        Stack<String> back = new Stack<>();\n        Stack<String> forward = new Stack<>();\n        String curr;\n    public BrowserHistory(String homepage) {\n        curr = homepage;\n    }\n    \n    public void visit(String url) {\n        back.push(curr);\n        curr = url;\n        while(!forward.isEmpty()) {\n            forward.pop();\n        }\n    }\n    \n    public String back(int steps) {\n        for(int i=0;i<steps && !back.isEmpty();i++) {\n            forward.push(curr);\n            curr = back.peek();\n            back.pop();\n        }\n        return curr;\n    }\n    \n    public String forward(int steps) {\n        for(int i=0;i<steps && !forward.isEmpty();i++) {\n            back.push(curr);\n            curr = forward.pop();\n        }\n        return curr;\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```
['Stack', 'Java']