Reverse a String Using a Stack
Problem Reverse a given string using an explicit stack data structure, without using built-in reverse functions or two-pointer swapping.
Input / Output
- Input: a string
s - Output: the reversed string
Constraints
- 0 <= |s| <= 10^5
- Must use an explicit stack — the point is to demonstrate LIFO behaviour, not to find the fastest reversal
- O(n) time, O(n) extra space for the stack
Example
- Input:
"hello"→ Output:"olleh" - Input:
"ab"→ Output:"ba"; empty string returns empty string
asked …