Validate Stack Sequences

Medium

Description

Given two integer sequences pushed and popped, return true if this could have been the result of push/pop operations on an initially empty stack.

Examples

Input:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
Output:true
Explanation:

Valid sequence.

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

For pushed = [1,2,3,4,5], popped = [4,3,5,1,2], the answer is false because the validate stack sequences condition is not met.

Input:pushed = [1,2,3], popped = [3,1,2]
Output:false
Explanation:

After pushing 1,2,3 and popping 3, the stack contains [1,2]. To pop 1 next, the task requires pop 2 first, but the sequence expects 1 before 2, making this sequence invalid.

Constraints

  • 1 ≤ n ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!