Problem Statement:-
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
.
Link: https://leetcode.com/problems/design-browser-history/
Problem Explanation with examples:-
Example 1
Input:
["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"]
[["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]]
Output:
[null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"]
Explanation:
BrowserHistory browserHistory = new BrowserHistory("leetcode.com");
browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com"
browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com"
browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com"
browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com"
browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com"
browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com"
browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com"
browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps.
browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com"
browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"
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
.
Intuition:-
A list can be used to store the history of the browser.
A separate index variable can be used to keep track of the current page.
The list can be truncated to the current page index + 1 when a new page is visited to remove the forward history.
The index can be incremented or decremented based on the number of steps and the length of the list, keeping it within the bounds of the list.
Solution:-
Create a list to store the history of the browser and assign it to the object.
Create an index variable to keep track of the current page and assign it to the object. Initialize it to 0.
When a new page is visited, insert the new page at the index + 1 position in the list. Increment the index by 1.
Truncate the list to the index + 1 position to remove the forward history using the list slicing syntax.
When the back button is pressed, decrement the index by the number of steps. If the number of steps is greater than the index, set the index to 0. Else, set the index to the index - steps.
When the forward button is pressed, increment the index by the number of steps. If the number of steps is greater than the length of the list - index - 1, set the index to the length of the list - 1. Else, set the index to the index + steps.
Return the page at the index position in the list in both the back and forward functions.
Code:-
JAVA Solution
public class BrowserHistory {
private List<String> browser;
private int idx;
public BrowserHistory(String homepage) {
browser = new ArrayList<>();
browser.add(homepage);
idx = 0;
}
public void visit(String url) {
browser.add(idx+1, url);
idx += 1;
browser = browser.subList(0, idx+1);
}
public String back(int steps) {
if (steps > idx) {
idx = 0;
} else {
idx -= steps;
}
return browser.get(idx);
}
public String forward(int steps) {
if (steps > (browser.size() - idx - 1)) {
idx = browser.size() - 1;
} else {
idx += steps;
}
return browser.get(idx);
}
}
Python Solution
class BrowserHistory:
def __init__(self, homepage: str):
self.browser = [homepage]
self.idx = 0
def visit(self, url: str) -> None:
self.browser.insert(self.idx+1,url)
self.idx += 1
self.browser = self.browser[:self.idx+1]
def back(self, steps: int) -> str:
if steps > self.idx:
self.idx = 0
else:
self.idx -= steps
return self.browser[self.idx]
def forward(self, steps: int) -> str:
if steps > (len(self.browser) - self.idx - 1):
self.idx = len(self.browser)-1
else:
self.idx += steps
return self.browser[self.idx]
Complexity Analysis:-
TIME:-
visit
operation has a time complexity of O(n) as it involves inserting a new element at a specified position and requires shifting all the elements to the right of the position by one index.back
andforward
operations have a time complexity of O(1) as they only require accessing the element at the specified index.
SPACE:-
The space complexity of the BrowserHistory
class is O(n), where n is the number of URLs stored in the browser list. This is because we are storing all the visited URLs in the browser
list. The idx
variable also takes constant space.