 All Problems
Validate Stack Sequences
medium
stack
arrays
simulation
amazon
google
facebook

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.

Example 1:

Input:
1 2 3 4 5
4 5 3 2 1
Output: true

Example 2:

Input:
1 2 3 4 5
4 3 5 1 2
Output: false

Constraints:

  • 1 ≤ pushed.length ≤ 1000
  • 0 ≤ pushed[i] ≤ 1000
  • All values of pushed are unique.
  • popped.length == pushed.length

Input format: First line: pushed. Second line: popped.

Output format: true or false.

Run to check your code against the sample cases, or submit to run every case