Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise.

&nbsp;
Example 1:

Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
Output: true
Explanation: We might do the following sequence:
push(1), push(2), push(3), push(4),
pop() -&gt; 4,
push(5),
pop() -&gt; 5, pop() -&gt; 3, pop() -&gt; 2, pop() -&gt; 1


Example 2:

Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
Output: false
Explanation: 1 cannot be popped before 2.


&nbsp;
Constraints:


	1 &lt;= pushed.length &lt;= 1000
	0 &lt;= pushed[i] &lt;= 1000
	All the elements of pushed are unique.
	popped.length == pushed.length
	popped is a permutation of pushed.


class Solution:
    def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
        stack=[]
        i=0
        for num in pushed:
            stack.append(num) #we are pushing the number to the stack
            while  len(stack) >0 and stack[len(stack)-1] == popped[i] :
                #if the last element of the stack is equal to the popped element
                stack.pop()
                i+=1 #we are incrementing i
        return True if len(stack) ==0 else False
		
class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> st = new Stack<>(); // Create a stack

        int j = 0; // Intialise one pointer pointing on popped array

        for(int val : pushed){
            st.push(val); // insert the values in stack
            while(!st.isEmpty() && st.peek() == popped[j]){ // if st.peek() values equal to popped[j];
                st.pop(); // then pop out
                j++; // increment j
            }
        }
        return st.isEmpty(); // check if stack is empty return true else false
    }
}
class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        stack<int> st; // Create a stack
        
        int j = 0; // Intialise one pointer pointing on popped array
        
        for(auto val : pushed){
            st.push(val); // insert the values in stack
            while(st.size() > 0 && st.top() == popped[j]){ // if st.peek() values equal to popped[j];
                st.pop(); // then pop out
                j++; // increment j
            }
        }
        return st.size() == 0; // check if stack is empty return true else false
    }
};
var validateStackSequences = function(pushed, popped) {
    let stack = [];
    let j = 0;
    
    for(let i=0; i<pushed.length; i++){
        stack.push(pushed[i]);
        while(stack.length != 0 && popped[j]== stack[stack.length-1]){
            stack.pop();
            j++
        }
    }
    return stack.length<1

};
Validate Stack Sequences