Skip to content

Latest commit

 

History

History
70 lines (48 loc) · 1.28 KB

File metadata and controls

70 lines (48 loc) · 1.28 KB
title difficulty tags
Reverse a String Using Stack
🟢 Easy
String
Stack

Reverse a String Using Stack

Problem Description

Given a string, write a function that uses a stack to reverse the string. Return the reversed string.

Examples

Example 1:

Input: "Hello, World!"
Output: "!dlroW ,olleH"

Example 2:

Input: "OpenAI"
Output: "IAnepO"

Example 3:

Input: "Stacks are fun!"
Output: "!nuf era skcatS"

Constraints

  • 1 <= s.length <= 10⁵
  • s[i] is a printable ASCII character

Solution

Intuition

A stack reverses order naturally: first in, last out. Push all characters onto the stack, then pop them off to build the reversed string.

Algorithm

  1. Push all characters onto the stack
  2. Pop each character and append to result
  3. Join and return

Complexity Analysis

  • Time Complexity: $O(n)$ — push and pop each character once
  • Space Complexity: $O(n)$ — stack holds all characters
class Solution:
    def reverseString(self, s: str) -> str:
        stack = list(s)  # push all characters
        reversed_list = []

        while stack:
            reversed_list.append(stack.pop())  # LIFO: last char comes out first

        return ''.join(reversed_list)