Intuition
Since we need reversing behaviors - more like, switching reversing behaviors this is a good instance of pulling out the good’ol deque
OR a double ended queue. Basically, you can insert data via both the front and the back.
So in the problem, when you come across an i
switch a flag - and now start inserting from the back into the deque. While returning, check with the flag to see which state it is on and return accordingly.
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is since we would be visiting all characters in the string of length N.
-
Memory
The memory usage is since we are using a
deque
to store the string.
— A