Given push[] and pop[] sequences with distinct values. The task is to check if this could have been the result of a sequence of push and pop operations on an initially empty stack. Return “True” if it otherwise returns “False”.
Examples:
Input: pushed = { 1, 2, 3, 4, 5 }, popped = { 4, 5, 3, 2, 1 }
Output: True
Following sequence can be performed:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Input: pushed = { 1, 2, 3, 4, 5 }, popped = { 4, 3, 5, 1, 2 }
Output: False
1 can't be popped before 2.
Approach: If the element X has been pushed to the stack then check if the top element in the pop[] sequence is X or not. If it is X then pop it right now else top of the push[] sequence will be changed and make the sequences invalid. So, similarly, do the same for all the elements and check if the stack is empty or not in the last. If empty then print True else print False.
Below is the implementation of above approach:
C++
#include <iostream>
#include <stack>
using namespace std;
bool validateStackSequence( int pushed[], int popped[], int len){
int j = 0;
stack < int > st;
for ( int i = 0; i < len; i++){
st.push(pushed[i]);
while (!st.empty() && j < len && st.top() == popped[j]){
st.pop();
j++;
}
}
return j == len;
}
int main()
{
int pushed[] = {1, 2, 3, 4, 5};
int popped[] = {4, 5, 3, 2, 1};
int len = sizeof (pushed)/ sizeof (pushed[0]);
cout << (validateStackSequence(pushed, popped, len) ? "True" : "False" );
return 0;
}
|
Java
import java.util.*;
class GfG
{
static boolean validateStackSequence( int pushed[],
int popped[], int len)
{
int j = 0 ;
Stack<Integer> st = new Stack<>();
for ( int i = 0 ; i < len; i++)
{
st.push(pushed[i]);
while (!st.empty() && j < len &&
st.peek() == popped[j])
{
st.pop();
j++;
}
}
return j == len;
}
public static void main(String[] args)
{
int pushed[] = { 1 , 2 , 3 , 4 , 5 };
int popped[] = { 4 , 5 , 3 , 2 , 1 };
int len = pushed.length;
System.out.println((validateStackSequence(pushed, popped, len) ? "True" : "False" ));
}
}
|
Python3
def validateStackSequence(pushed, popped):
j = 0
stack = []
for x in pushed:
stack.append(x)
while stack and j < len (popped) and stack[ - 1 ] = = popped[j]:
stack.pop()
j = j + 1
return j = = len (popped)
pushed = [ 1 , 2 , 3 , 4 , 5 ]
popped = [ 4 , 5 , 3 , 2 , 1 ]
print (validateStackSequence(pushed, popped))
|
C#
using System;
using System.Collections.Generic;
class GfG
{
static bool validateStackSequence( int []pushed,
int []popped, int len)
{
int j = 0;
Stack< int > st = new Stack< int >();
for ( int i = 0; i < len; i++)
{
st.Push(pushed[i]);
while (st.Count != 0 && j < len &&
st.Peek() == popped[j])
{
st.Pop();
j++;
}
}
return j == len;
}
public static void Main(String[] args)
{
int []pushed = {1, 2, 3, 4, 5};
int []popped = {4, 5, 3, 2, 1};
int len = pushed.Length;
Console.WriteLine((validateStackSequence(pushed,
popped, len) ? "True" : "False" ));
}
}
|
PHP
<?php
function validateStackSequence( $pushed , $popped , $len )
{
$j = 0;
$st = array ();
for ( $i = 0; $i < $len ; $i ++)
{
array_push ( $st , $pushed [ $i ]);
while (! empty ( $st ) && $j < $len &&
$st [ count ( $st ) - 1] == $popped [ $j ])
{
array_pop ( $st );
$j ++;
}
}
return $j == $len ;
}
$pushed = array (1, 2, 3, 4, 5);
$popped = array (4, 5, 3, 2, 1);
$len = count ( $pushed );
echo (validateStackSequence( $pushed ,
$popped , $len ) ? "True" : "False" );
?>
|
Javascript
<script>
function validateStackSequence( pushed, popped, len)
{
var j = 0;
var st=[];
for ( var i = 0; i < len; i++){
st.push(pushed[i]);
while (!st.length==0 && j < len &&
st[st.length-1] == popped[j])
{
st.pop();
j++;
}
}
return j == len;
}
var pushed = [1, 2, 3, 4, 5];
var popped = [4, 5, 3, 2, 1];
var len = pushed.length;
document.write( (validateStackSequence(pushed, popped, len)
? "True" : "False" ));
</script>
|
Time Complexity: O(N), where N is size of stack.