The problem can be solved by using a stack.
- We push everything in the stack.
- We pop everything off the stack
The running time of this algorithm is bounded by the stack operations. We can
achieve O(n)
.
The problem can be solved by using a stack.
The running time of this algorithm is bounded by the stack operations. We can
achieve O(n)
.
""" | |
mirror_image.py | |
""" | |
def mirror(string): | |
""" | |
mirror takes a str string. | |
mirror returns a str that is the reversed string. | |
""" | |
reverse = [] | |
for character in string: | |
reverse.append(character) | |
output = "" | |
while reverse: | |
output += reverse.pop() | |
return output |