Check if two Stacks are equal or not without alteration
Given two Stacks S1 and S2, the task is to check if both the stacks are equal or not in the same order without losing the original stacks. If both the stacks are equal, then print “Yes “. Otherwise, print “No”.
Examples:
Input: S1 = {3, 4, 2, 1}, S2 = {3, 4, 2, 1}
Output: Yes
Input: S1 = {3, 4, 6}, S2 = {7, 2, 1}
Output: No
Approach: The given problem can be solved by moving some amount of elements between the given two stacks for checking each corresponding element in the two stacks. Follow the steps below to solve the problem:
- Store the size of stack S1 and S2, in a variable N and M respectively.
- If N is not equal to M, then print “No” and return.
- Iterate over the range [1, N] and perform the following operations:
- After completing the above steps, if none of the above cases satisfy, then print “Yes“.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void pushElements(stack< int > s1, stack< int > s2, int len)
{
int i = 1;
while (i <= len) {
if (s1.size() > 0) {
s2.push(s1.top());
s1.pop();
}
i++;
}
}
string compareStacks(stack< int > s1, stack< int > s2)
{
int N = s1.size();
int M = s2.size();
if (N != M) {
return "No" ;
}
for ( int i = 1; i <= N; i++) {
pushElements(s1, s2, N - i);
int val = s1.top();
pushElements(s2, s1, 2 * (N - i));
if (val != s2.top())
return "No" ;
pushElements(s1, s2, N - i);
}
return "Yes" ;
}
int main()
{
stack< int > S1, S2;
S1.push(1);
S1.push(2);
S1.push(4);
S1.push(3);
S2.push(1);
S2.push(2);
S2.push(4);
S2.push(3);
cout << (compareStacks(S1, S2));
}
|
Java
import java.util.*;
class GFG {
static String compareStacks(
Stack<Integer> s1,
Stack<Integer> s2)
{
int N = s1.size();
int M = s2.size();
if (N != M) {
return "No" ;
}
for ( int i = 1 ; i <= N; i++) {
pushElements(s1, s2, N - i);
int val = s1.peek();
pushElements(s2, s1, 2 * (N - i));
if (val != s2.peek())
return "No" ;
pushElements(s1, s2, N - i);
}
return "Yes" ;
}
static void pushElements(
Stack<Integer> s1, Stack<Integer> s2,
int len)
{
int i = 1 ;
while (i <= len) {
s2.push(s1.pop());
i++;
}
}
public static void main(String[] args)
{
Stack<Integer> S1 = new Stack<>();
Stack<Integer> S2 = new Stack<>();
S1.push( 1 );
S1.push( 2 );
S1.push( 4 );
S1.push( 3 );
S2.push( 1 );
S2.push( 2 );
S2.push( 4 );
S2.push( 3 );
System.out.println(
compareStacks(S1, S2));
}
}
|
Python3
def compareStacks(s1, s2):
N = len (s1)
M = len (s2)
if (N ! = M):
return "No"
for i in range ( 1 , N + 1 ):
pushElements(s1, s2, N - i)
val = s1[ - 1 ]
pushElements(s2, s1, 2 * (N - i))
if (val ! = s2[ - 1 ]):
return "No"
pushElements(s1, s2, N - i)
return "Yes"
def pushElements(s1, s2, len ):
i = 1
while (i < = len ):
s2.append(s1[ - 1 ])
del s1[ - 1 ]
i + = 1
if __name__ = = '__main__' :
S1 = []
S2 = []
S1.append( 1 )
S1.append( 2 )
S1.append( 4 )
S1.append( 3 )
S2.append( 1 )
S2.append( 2 )
S2.append( 4 )
S2.append( 3 )
print (compareStacks(S1, S2))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static String compareStacks(
Stack< int > s1,
Stack< int > s2)
{
int N = s1.Count;
int M = s2.Count;
if (N != M) {
return "No" ;
}
for ( int i = 1; i <= N; i++) {
pushElements(s1, s2, N - i);
int val = s1.Peek();
pushElements(s2, s1, 2 * (N - i));
if (val != s2.Peek())
return "No" ;
pushElements(s1, s2, N - i);
}
return "Yes" ;
}
static void pushElements(
Stack< int > s1, Stack< int > s2,
int len)
{
int i = 1;
while (i <= len) {
s2.Push(s1.Pop());
i++;
}
}
public static void Main(String[] args)
{
Stack< int > S1 = new Stack< int >();
Stack< int > S2 = new Stack< int >();
S1.Push(1);
S1.Push(2);
S1.Push(4);
S1.Push(3);
S2.Push(1);
S2.Push(2);
S2.Push(4);
S2.Push(3);
Console.WriteLine(
compareStacks(S1, S2));
}
}
|
Javascript
<script>
function pushElements(s1, s2, len)
{
var i = 1;
while (i <= len) {
if (s1.length > 0) {
s2.push(s1[s1.length-1]);
s1.pop();
}
i++;
}
}
function compareStacks(s1, s2)
{
var N = s1.length;
var M = s2.length;
if (N != M) {
return "No" ;
}
for ( var i = 1; i <= N; i++) {
pushElements(s1, s2, N - i);
var val = s1[s1.length-1];
pushElements(s2, s1, 2 * (N - i));
if (val != s2[s2.length-1])
return "No" ;
pushElements(s1, s2, N - i);
}
return "Yes" ;
}
var S1 = [], S2 = [];
S1.push(1);
S1.push(2);
S1.push(4);
S1.push(3);
S2.push(1);
S2.push(2);
S2.push(4);
S2.push(3);
document.write(compareStacks(S1, S2));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
20 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...