✅✅ Using Stacks || Beginner Friendly || Easy Solution
IntuitionTo simulate browser history behavior, we need to efficiently track pages visited before and after the current one. Two stacks make this easy — one for
# Intuition
To simulate browser history behavior, we need to efficiently track pages visited before and after the current one. Two stacks make this easy — one for the past (back history) and one for the future (forward history). A separate `curr` string keeps track of the current page.

# Approach
- Use two stacks:  
  - `past` stack stores the history before the current page.
  - `future` stack stores pages after the current page when the user goes back.
- On visiting a new URL:
  - Push the current page to `past`.
  - Clear the `future` stack.
- On going back:
  - Pop from `past` and push the current page to `future`.
  - Repeat until steps are 0 or `past` is empty.
- On going forward:
  - Pop from `future` and push the current page to `past`.
  - Repeat until steps are 0 or `future` is empty.

# Time Complexity
- `visit(url)`: O(1)  
- `back(steps)`: O(steps)  
- `forward(steps)`: O(steps)

# Space Complexity
- O(n), where n is the total number of pages visited.  
  Each visited page can be stored in either `past`, `future`, or `curr`.

# Code
```cpp []
class BrowserHistory {
public:
    stack<string>past;
    stack<string>future;
    string curr;
    BrowserHistory(string homepage) {
        curr=homepage;
    }
    
    void visit(string url) {
        past.push(curr);
        curr=url;
        future=stack<string>();
    }
    
    string back(int steps) {
        while(!past.empty() && steps--){
            future.push(curr);
            curr=past.top();
            past.pop();
        }
        return curr;
    }
    
    string forward(int steps) {
        while(!future.empty() && steps--){
            past.push(curr);
            curr=future.top();
            future.pop();
        }
        return curr;
    }
};
```
# Have a Good Day 😊 UpVote?
['Array', 'Stack', 'Design', 'Data Stream', 'C++']