Design Browser History

Design Browser History

Leetcode Daily Challenge (18th March, 2023)

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 the homepage of the browser.

  • void visit(string url) Visits url from the current page. It clears up all the forward history.

  • string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps.

  • string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.

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 and url consist of '.' or lower case English letters.

  • At most 5000 calls will be made to visit, back, and forward.

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 and forward 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.

References:-

Connect with me:-

Did you find this article valuable?

Support Leeting-LCS by becoming a sponsor. Any amount is appreciated!