Imagine you're building the core navigation system for a web browser! ๐ You need to implement a BrowserHistory class that manages the browsing history with familiar operations like visiting new pages, going back, and moving forward.
Your browser starts on a homepage and supports these key operations:
- Visit a URL: Navigate to a new page (this clears any forward history, just like real browsers!)
- Go Back: Move back through your browsing history by a specified number of steps
- Go Forward: Move forward through your browsing history by a specified number of steps
The challenge is to efficiently track the current position and handle the dynamic nature of web browsing where visiting a new page from the middle of history erases all forward pages.
Example: Start at "homepage" โ visit "google.com" โ visit "youtube.com" โ go back 1 step (now at "google.com") โ visit "facebook.com" (this erases "youtube.com" from forward history)
Input & Output
Visualization
Time & Space Complexity
Back and forward are O(1) pointer movements. Visit is O(1) amortized for array operations.
Store at most n URLs in the history array where n is the number of pages visited
Constraints
- 1 โค homepage.length โค 20
- 1 โค url.length โค 20
- 1 โค steps โค 100
- homepage and url consist of '.' or lowercase English letters
- At most 5000 calls will be made to visit, back, and forward