
Leetcode Problem 1472: Design Browser History
Design a browser history system for a single-tab browser. The browser starts on a given homepage. visit(url) navigates to the given URL from the current page, clearing all forward history. back(steps) moves back at most the given number of steps in the history, returning the current URL after moving. If there are fewer pages in the back history than the requested steps, it moves back as far as possible. forward(steps) moves forward at most the given number of steps in the history, returning the current URL after moving. If there are fewer pages in the forward history than the requested steps, it moves forward as far as possible.
Constraints:
visit, back, and forward.class BrowserHistory {
constructor(
homepage: string,
private currentUrl: string = homepage,
private backStack: string[] = [],
private forwardStack: string[] = []
) { }
visit(url: string): void {
this.backStack.push(this.currentUrl)
this.currentUrl = url
this.forwardStack = []
}
back(steps: number): string {
let currentStep = steps
while (this.backStack.length > 0 && currentStep > 0) {
this.forwardStack.push(this.currentUrl)
this.currentUrl = this.backStack.pop()!
currentStep--
}
return this.currentUrl
}
forward(steps: number): string {
let currentStep = steps
while (this.forwardStack.length > 0 && currentStep > 0) {
this.backStack.push(this.currentUrl)
this.currentUrl = this.forwardStack.pop()!
currentStep--
}
return this.currentUrl
}
}