[Java] Elegant O(n) Solution with 2 Stacks - Comments Included
n accounts to the number of steps for back and forward method inputs.\n\nclass BrowserHistory {\n\n    private Stack<String> back;\n    private Stack<String> fo
n accounts to the number of steps for back and forward method inputs.\n```\nclass BrowserHistory {\n\n    private Stack<String> back;\n    private Stack<String> forward;\n    \n    public BrowserHistory(String homepage) {\n        back = new Stack<>();\n        forward = new Stack<>();\n        back.push(homepage);\n    }\n    \n    public void visit(String url) {\n        back.push(url);\n\t\t// if I visit a new url, there is no more going forward. Clear the forward stack.\n        forward.clear();\n    }\n    \n    public String back(int steps) {\n        int count = 0;\n\t\t\n\t\t// go back in history for given number of steps. To make sure that you can forward to each url, keep them piled up in the forward stack\n        while(count < steps && back.size() > 1){\n            count++;\n            forward.push(back.pop());\n        }\n        return back.peek();\n    }\n    \n    public String forward(int steps) {\n        int count = 0;\n\t\t\n\t\t//go forward in history for given number of steps. To make sure you can go back to each url, keep them piled in the back stack\n        while(count < steps && forward.size() > 0){\n            count++;\n            back.push(forward.pop());\n        }\n        return back.peek();\n    }\n    \n    \n}\n\n```
['Java']