Design Browser History
Design Browser History
You have a
browser
of one tab where you start on the
homepage and
you can visit another
url, get back
in the history number of
steps or move
forward in the history number of
steps.
Implement the
BrowserHistory
class:
-
BrowserHistory(string homepage)Initializes the object with thehomepageof the browser. -
void visit(string url)Visitsurlfrom the current page. It clears up all the forward history. -
string back(int steps)Movestepsback in history. If you can only returnxsteps in the history andsteps > x, you will return onlyxsteps. Return the currenturlafter moving back in history at moststeps. -
string forward(int steps)Movestepsforward in history. If you can only forwardxsteps in the history andsteps > x, you will forward onlyxsteps. Return the currenturlafter forwarding in history at moststeps.
Constraints:
-
1 <= homepage.length <= 20 -
1 <= url.length <= 20 -
1 <= steps <= 100 -
homepageandurlconsist of '.' or lower case English letters. -
At most
5000calls will be made tovisit,back, andforward.
Practise: https://leetcode.com/problems/design-browser-history/
Solution
Using dynamic array
Time: O(1) for
each method
Space:
O(N), where N
is the most URLs we save