Stack Permutations (Check if a queue is stack permutation of other)
Last Updated :
28 Mar, 2025
A stack permutation refers to the rearrangement of elements from an input queue to an output queue using a stack, where elements are transferred with the help of stack operations like push and pop. The rules are as follows:
- Dequeue elements only from the input queue.
- Utilize the stack’s built-in push and pop functions.
- Output Queue is initially empty and we need to fill the elements in desired order.
- Elements can only be dequeued from the input queue and enqueued into the output queue.
Since stack operations allow a variety of element sequences, a large number of permutations are possible for a single input queue.
Given two arrays, a[] and b[], where both arrays consist of unique elements:
a[]
represents the input queue.b[]
represents the desired output queue.
We need to check whether b[] can be obtained by performing stack permutation on a[]. This means that we can push and pop elements from the input queue (a[]) to an auxiliary stack, and then pop elements from the stack to form the output queue (b[]).
Examples:
Input: a[] = [ 1, 2, 3 ] , b[] = [ 2, 1, 3 ]
Output: True
Explanation:
1. push 1 from a to stack
2. push 2 from b to stack
3. pop 2 from stack to b
4. pop 1 from stack to b
5. push 3 from arr1 to stack
6. pop 3 from stack to b
Input: a[] = [ 1, 2, 3 ] , b[] = [ 3, 1, 2 ]
Output: False
Explanation: Not Possible
[Naive Approach] - Creating an Actual Queue - O(n) time and O(n) space
We create both input and desired output queues using given arrays. We dequeue items one by one from input queue and it the dequeued item matches the front of output queue, then we remove from both queues. We also remove top of the stack and output queue if they match. Otherwise we push the dequeued item into the stack.
Please note that we only need to find out whether the input queue is permutable or not.
C++
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
bool checkPerm(vector<int>& a, vector<int>& b)
{
queue<int> q1;
for (int i = 0; i < a.size(); i++) q1.push(a[i]);
queue<int> q2;
for (int i = 0; i < b.size(); i++) q2.push(b[i]);
stack<int> st;
// Dequeue all items one by one
while (!q1.empty())
{
int ele = q1.front();
q1.pop();
if (ele == q2.front())
{
// If matches, then dequeue from
// the output queue also
q2.pop();
// Now see if top of the stack
// also matches the front
while (!st.empty())
{
if (st.top() == q2.front())
{
st.pop();
q2.pop();
}
else
break;
}
}
else
st.push(ele);
}
bool res = (q1.empty() && st.empty());
cout << (res ? "True" : "False") << endl;
return res;
}
int main()
{
vector<int> a = {1, 2, 3};
vector<int> b = {2, 1, 3};
checkPerm(a, b);
return 0;
}
Java
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class GfG {
public static boolean checkPerm(int[] a, int[] b) {
Queue<Integer> q1 = new LinkedList<>();
for (int i = 0; i < a.length; i++) q1.offer(a[i]);
Queue<Integer> q2 = new LinkedList<>();
for (int i = 0; i < b.length; i++) q2.offer(b[i]);
Stack<Integer> st = new Stack<>();
// Dequeue all items one by one
while (!q1.isEmpty()) {
int ele = q1.poll();
if (ele == q2.peek()) {
// If matches, then dequeue from
// the output queue also
q2.poll();
// Now see if top of the stack
// also matches the front
while (!st.isEmpty()) {
if (st.peek() == q2.peek()) {
st.pop();
q2.poll();
} else
break;
}
} else
st.push(ele);
}
boolean res = (q1.isEmpty() && st.isEmpty());
System.out.println(res ? "True" : "False");
return res;
}
public static void main(String[] args) {
int[] a = {1, 2, 3};
int[] b = {2, 1, 3};
checkPerm(a, b);
}
}
Python
from collections import deque
def checkPerm(a, b):
q1 = deque(a)
q2 = deque(b)
st = []
# Dequeue all items one by one
while q1:
ele = q1.popleft()
if ele == q2[0]:
# If matches, then dequeue
# from the output queue also
q2.popleft()
# Now see if top of the stack
# also matches the front
while st and st[-1] == q2[0]:
st.pop()
q2.popleft()
else:
st.append(ele)
res = (not q1 and not st)
print("True" if res else "False")
return res
if __name__ == '__main__':
a = [1, 2, 3]
b = [2, 1, 3]
checkPerm(a, b)
C#
using System;
using System.Collections.Generic;
class Program {
static bool CheckPerm(List<int> a, List<int> b) {
Queue<int> q1 = new Queue<int>();
foreach (var item in a) q1.Enqueue(item);
Queue<int> q2 = new Queue<int>();
foreach (var item in b) q2.Enqueue(item);
Stack<int> st = new Stack<int>();
// Dequeue all items one by one
while (q1.Count > 0) {
int ele = q1.Dequeue();
if (ele == q2.Peek()) {
// If matches, then dequeue from
// the output queue also
q2.Dequeue();
// Now see if top of the stack
// also matches the front
while (st.Count > 0) {
if (st.Peek() == q2.Peek()) {
st.Pop();
q2.Dequeue();
} else
break;
}
} else
st.Push(ele);
}
bool res = (q1.Count == 0 && st.Count == 0);
Console.WriteLine(res ? "True" : "False");
return res;
}
static void Main() {
List<int> a = new List<int> { 1, 2, 3 };
List<int> b = new List<int> { 2, 1, 3 };
CheckPerm(a, b);
}
}
JavaScript
function checkPerm(a, b) {
let q1 = [...a];
let q2 = [...b];
let st = [];
// Dequeue all items one by one
while (q1.length > 0) {
let ele = q1.shift();
if (ele === q2[0]) {
// If matches, then dequeue from the output queue also
q2.shift();
// Now see if top of the stack also matches the front
while (st.length > 0 && st[st.length - 1] === q2[0]) {
st.pop();
q2.shift();
}
} else {
st.push(ele);
}
}
let res = (q1.length === 0 && st.length === 0);
console.log(res ? "True" : "False");
return res;
}
let a = [1, 2, 3];
let b = [2, 1, 3];
checkPerm(a, b);
[Expected Approach] Simulating Push and Pop - O(n) time and O(1) space
The idea is same as the above approach, but we not create queues here and do not change input arrays. It simulates the process of pushing elements from a[] onto a stack and popping them in the order required to match b[]. For each element in a[], it is pushed onto the stack, and then the top of the stack is compared to the current element of b[]. If they match, the element is popped from the stack and the next element in b[] is checked. The process continues until all elements of a[] are processed. If all elements in b[] can be matched and popped from the stack, it returns True (valid permutation), otherwise False
(invalid permutation)
C++
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
bool checkPerm(vector<int>& a, vector<int>& b) {
stack<int> st;
int j = 0;
// Traverse a[]
for (int i = 0; i < a.size(); i++) {
// Push top of a[] to stack
st.push(a[i]);
// Keep popping from stack while it
// matches front of the output queue
while (!st.empty() && st.top() == b[j]) {
st.pop();
j++;
}
}
return (j == b.size());
}
int main() {
vector<int> a = {1, 2, 3};
vector<int> b = {2, 1, 3};
cout << (checkPerm(a, b) ? "True" : "False") << endl;
return 0;
}
Java
import java.util.Stack;
public class GfG {
public static boolean checkPerm(int[] a, int[] b) {
Stack<Integer> st = new Stack<>();
int j = 0;
// Traverse a[]
for (int i = 0; i < a.length; i++) {
// Push top of a[] to stack
st.push(a[i]);
// Keep popping from stack while it
// matches front of the output array
while (!st.isEmpty() && st.peek().equals(b[j])) {
st.pop();
j++;
}
}
return (j == b.length);
}
public static void main(String[] args) {
int[] a = {1, 2, 3};
int[] b = {2, 1, 3};
System.out.println(checkPerm(a, b) ? "True" : "False");
}
}
Python
def checkPerm(a, b):
stack = []
j = 0
# Traverse a[]
for i in range(len(a)):
# Push top of a[] to stack
stack.append(a[i])
# Keep popping from stack while it
# matches front of the output queue
while stack and stack[-1] == b[j]:
stack.pop()
j += 1
return j == len(b)
if __name__ == '__main__':
a = [1, 2, 3]
b = [2, 1, 3]
print("True" if checkPerm(a, b) else "False")
C#
using System;
using System.Collections.Generic;
class Program {
static bool CheckPerm(List<int> a, List<int> b) {
Stack<int> st = new Stack<int>();
int j = 0;
// Traverse a[]
for (int i = 0; i < a.Count; i++) {
// Push top of a[] to stack
st.Push(a[i]);
// Keep popping from stack while it
// matches front of the output queue
while (st.Count > 0 && st.Peek() == b[j]) {
st.Pop();
j++;
}
}
return (j == b.Count);
}
static void Main() {
List<int> a = new List<int> { 1, 2, 3 };
List<int> b = new List<int> { 2, 1, 3 };
Console.WriteLine(CheckPerm(a, b) ? "True" : "False");
}
}
JavaScript
function checkPerm(a, b) {
const stack = [];
let j = 0;
// Traverse a[]
for (let i = 0; i < a.length; i++) {
// Push top of a[] to stack
stack.push(a[i]);
// Keep popping from stack while it
// matches front of the output queue
while (stack.length > 0 && stack[stack.length - 1] === b[j]) {
stack.pop();
j++;
}
}
return j === b.length;
}
const a = [1, 2, 3];
const b = [2, 1, 3];
console.log(checkPerm(a, b) ? 'True' : 'False');
Similar Reads
Basics & Prerequisites
Data Structures
Getting Started with Array Data StructureArray is a collection of items of the same variable type that are stored at contiguous memory locations. It is one of the most popular and simple data structures used in programming. Basic terminologies of ArrayArray Index: In an array, elements are identified by their indexes. Array index starts fr
14 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem