---
tags: leetcode
---
# [1472. Design Browser History](https://leetcode.com/problems/design-browser-history/)
---
# My Solution
## The Key Idea for Solving This Coding Question
## C++ Code
```cpp=
class BrowserHistory {
public:
BrowserHistory(string homepage) {
currPage = homepage;
}
void visit(string url) {
backHistory.push(currPage);
currPage = url;
while (!forwardHistory.empty()) {
forwardHistory.pop();
}
}
string back(int steps) {
while (!backHistory.empty() && steps > 0) {
forwardHistory.push(currPage);
currPage = backHistory.top();
backHistory.pop();
--steps;
}
return currPage;
}
string forward(int steps) {
while (!forwardHistory.empty() && steps > 0) {
backHistory.push(currPage);
currPage = forwardHistory.top();
forwardHistory.pop();
--steps;
}
return currPage;
}
private:
stack<string> backHistory;
stack<string> forwardHistory;
string currPage;
};
/**
* Your BrowserHistory object will be instantiated and called as such:
* BrowserHistory* obj = new BrowserHistory(homepage);
* obj->visit(url);
* string param_2 = obj->back(steps);
* string param_3 = obj->forward(steps);
*/
```
## Time Complexity
## Space Complexity
# Miscellaneous
<!--
# Test Cases
```
["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]]
```
* Wrong Answer:
```
["BrowserHistory","visit","back","back","forward","forward","visit","visit","back"]
[["zav.com"],["kni.com"],[7],[7],[5],[1],["pwrrbnw.com"],["mosohif.com"],[9]]
```
-->