Check if the two given stacks are same
Last Updated :
01 Nov, 2023
Given two Stacks, the task is to check if the given stacks are same or not.
Two stacks are said to be same if they contains the same elements in the same order.
Example:
Approach:
- Take a flag variable and set it to true initially, flag = true. This variable will indicate whether the stacks are same or not.
- First check if the size of given stack1 and stack2 are equal. If the size is not equal, set flag to false and return it.
- If the size is same, then compare the top elements of both of the given stacks.
- If the top of both stacks is NOT same, set flag to false and return it otherwise pop top elements of both stacks.
- Repeat step 3 and 4 until all elements are popped out from both of the stacks.
- If both stacks gets empty and the flag variable is still true, it means that the stacks are same.
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
bool isSameStack(stack<string> stack1, stack<string> stack2)
{
bool flag = true ;
if (stack1.size() != stack2.size()) {
flag = false ;
return flag;
}
while (stack1.empty() == false ) {
if (stack1.top() == stack2.top()) {
stack1.pop();
stack2.pop();
}
else {
flag = false ;
break ;
}
}
return flag;
}
int main()
{
stack<string> stack1;
stack<string> stack2;
stack1.push( "Geeks" );
stack1.push( "4" );
stack1.push( "Geeks" );
stack1.push( "Welcomes" );
stack1.push( "You" );
stack2.push( "Geeks" );
stack2.push( "4" );
stack2.push( "Geeks" );
stack2.push( "Welcomes" );
stack2.push( "You" );
if (isSameStack(stack1, stack2))
cout << "Stacks are Same" ;
else
cout << "Stacks are not Same" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean isSameStack(Stack<String> stack1,
Stack<String> stack2)
{
boolean flag = true ;
if (stack1.size() != stack2.size())
{
flag = false ;
return flag;
}
while (stack1.empty() == false )
{
if (stack1.peek() == stack2.peek())
{
stack1.pop();
stack2.pop();
}
else
{
flag = false ;
break ;
}
}
return flag;
}
public static void main(String arr[])
{
Stack<String> stack1 = new Stack<String>();
Stack<String> stack2 = new Stack<String>();
stack1.push( "Geeks" );
stack1.push( "4" );
stack1.push( "Geeks" );
stack1.push( "Welcomes" );
stack1.push( "You" );
stack2.push( "Geeks" );
stack2.push( "4" );
stack2.push( "Geeks" );
stack2.push( "Welcomes" );
stack2.push( "You" );
if (isSameStack(stack1, stack2))
System.out.println( "Stacks are Same" );
else
System.out.println( "Stacks are not Same" );
}
}
|
Python3
def isSameStack(stack1, stack2) :
flag = True ;
if ( len (stack1) ! = len (stack2)) :
flag = False ;
return flag;
while ( len (stack1)) :
if (stack1[ 0 ] = = stack2[ 0 ]) :
stack1.pop();
stack2.pop();
else :
flag = False ;
break ;
return flag;
if __name__ = = "__main__" :
stack1 = [];
stack2 = [];
stack1.append( "Geeks" );
stack1.append( "4" );
stack1.append( "Geeks" );
stack1.append( "Welcomes" );
stack1.append( "You" );
stack2.append( "Geeks" );
stack2.append( "4" );
stack2.append( "Geeks" );
stack2.append( "Welcomes" );
stack2.append( "You" );
if (isSameStack(stack1, stack2)) :
print ( "Stacks are Same" );
else :
print ( "Stacks are not Same" );
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static Boolean isSameStack(Stack<String> stack1,
Stack<String> stack2)
{
Boolean flag = true ;
if (stack1.Count != stack2.Count)
{
flag = false ;
return flag;
}
while (stack1.Count!=0)
{
if (stack1.Peek() == stack2.Peek())
{
stack1.Pop();
stack2.Pop();
}
else
{
flag = false ;
break ;
}
}
return flag;
}
public static void Main(String []arr)
{
Stack<String> stack1 = new Stack<String>();
Stack<String> stack2 = new Stack<String>();
stack1.Push( "Geeks" );
stack1.Push( "4" );
stack1.Push( "Geeks" );
stack1.Push( "Welcomes" );
stack1.Push( "You" );
stack2.Push( "Geeks" );
stack2.Push( "4" );
stack2.Push( "Geeks" );
stack2.Push( "Welcomes" );
stack2.Push( "You" );
if (isSameStack(stack1, stack2))
Console.WriteLine( "Stacks are Same" );
else
Console.WriteLine( "Stacks are not Same" );
}
}
|
Javascript
<script>
function isSameStack(stack1,stack2)
{
let flag = true ;
if (stack1.length != stack2.length)
{
flag = false ;
return flag;
}
while (stack1.length == false )
{
if (stack1[stack1.length-1] == stack2[stack2.length-1])
{
stack1.pop();
stack2.pop();
}
else
{
flag = false ;
break ;
}
}
return flag;
}
let stack1 = [];
let stack2 = [];
stack1.push( "Geeks" );
stack1.push( "4" );
stack1.push( "Geeks" );
stack1.push( "Welcomes" );
stack1.push( "You" );
stack2.push( "Geeks" );
stack2.push( "4" );
stack2.push( "Geeks" );
stack2.push( "Welcomes" );
stack2.push( "You" );
if (isSameStack(stack1, stack2))
document.write( "Stacks are Same" );
else
document.write( "Stacks are not Same" );
</script>
|
Time Complexity: O(N), as we are using a loop that will traverse N times in the worst case. Where N is the number of elements in the stacks.
Auxiliary Space: O(1), as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...