710056. Stack Permutations

Medium

Stack Permutations

Summary

Given a stack of integers, generate all possible permutations of its elements. This problem involves manipulating an array using a stack data structure, making it a combination of arrays and stacks.

Detailed Explanation

To solve this problem, we can use the concept of recursive backtracking to generate all possible permutations. We'll start by popping all elements from the stack and storing them in an array. Then, we'll use recursion to generate all permutations of the remaining stack (which will be an empty stack eventually).

Here's a step-by-step breakdown of the solution:

  1. Initialize an empty stack stack and a result list permutations.
  2. Pop all elements from the stack and store them in the permutations list.
  3. If the stack is empty, add the current permutation to the permutations list and return.
  4. Otherwise, recursively generate all permutations of the remaining stack (i.e., the stack with the last element removed).
  5. For each permutation generated in step 4, insert the last popped element at every possible position to create a new permutation.
  6. Add these new permutations to the permutations list and return.

Time complexity: O(n!), where n is the number of elements in the original stack. This is because we're generating all possible permutations of the elements. Space complexity: O(n), for storing the permutations.

Here's an ASCII art diagram illustrating the process:

  +---------------+
  |  Stack: [1, 2, 3] |
  +---------------+
           |
           |  Pop all elements
           v
  +---------------+
  | Permutations: [] |
  +---------------+
           |
           |  Recursively generate permutations of remaining stack
           v
  +---------------+
  | Permutations: [[1], [2], [3]] |
  +---------------+
           |
           |  Insert last popped element at every possible position
           v
  +---------------+
  | Permutations: [[1, 2], [1, 3], [2, 3], [1], [2], [3]] |
  +---------------+

Optimized Solutions

Java

View on GeeksforGeeks
import java.util.*;

public class StackPermutations {
    public List<List<Integer>> getStackPermutations(int[] stack) {
        List<List<Integer>> permutations = new ArrayList<>();
        backtrack(stack, new ArrayList<>(), permutations);
        return permutations;
    }

    private void backtrack(int[] stack, List<Integer> current, List<List<Integer>> permutations) {
        if (stack.length == 0) {
            permutations.add(new ArrayList<>(current));
            return;
        }
        for (int i = 0; i <= current.size(); i++) {
            current.add(stack[stack.length - 1]);
            backtrack(Arrays.copyOfRange(stack, 0, stack.length - 1), current, permutations);
            current.remove(current.size() - 1);
        }
    }

    public static void main(String[] args) {
        StackPermutations sp = new StackPermutations();
        int[] stack = {1, 2, 3};
        List<List<Integer>> permutations = sp.getStackPermutations(stack);
        System.out.println(permutations);
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.