Browser Back and Forward
A browser starts on the page home. commands is a list of strings applied in order:
"visit X"opens the pageX, which is placed right after the current page in the history. Every page that came after the current one (the pages you could go forward to) is forgotten."back K"movesKpages back in the history, or to the very first page if fewer are available."forward K"movesKpages forward in the history, or to the last page if fewer are available.
Return a list with the page you are on after each back and each forward command, in order. visit commands add nothing to the result.
Example 1
commands ["visit a","visit b","back 1","forward 1","visit c","back 2"]
returns ["a","b","a"]
After home, a, b: back 1 lands on a, forward 1 on b; visiting c makes the history home, a, b, c, and back 2 lands on a.
Example 2
commands ["back 3"]
returns ["home"]
There is only home, so going back 3 stays there.
Constraints
0 <= len(commands) <= 100- page names are 1 to 6 lowercase letters
1 <= K <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Stacks and queues problems
- Convoys at the GateMedium
- Daily TemperaturesMedium
- Decode StringMedium
- Flip and Double BracketsMedium
- Keep the Digits, One of Each LetterMedium
- Largest Open PlotMedium
- Longest Balanced PieceMedium
- Longest Steady StretchMedium