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 thehomepage
of the browser. -
void visit(string url)
Visitsurl
from the current page. It clears up all the forward history. -
string back(int steps)
Movesteps
back in history. If you can only returnx
steps in the history andsteps > x
, you will return onlyx
steps. Return the currenturl
after moving back in history at moststeps
. -
string forward(int steps)
Movesteps
forward in history. If you can only forwardx
steps in the history andsteps > x
, you will forward onlyx
steps. Return the currenturl
after forwarding in history at moststeps
.
Constraints:
-
1 <= homepage.length <= 20
-
1 <= url.length <= 20
-
1 <= steps <= 100
-
homepage
andurl
consist of '.' or lower case English letters. -
At most
5000
calls 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