X DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Stack Permutations
Medium Accuracy: 61.61% Submissions: 2454 Points: 4

You are given two arrays A and B of size N. Check if one array is a stack permutation of the other array or not.
Stack permutation means that one array can be created from another array using a stack and stack operations.

Example 1:

Input:
N = 3
A = {1,2,3}
B = {2,1,3}
Output:
1
Explanation:
1. push 1 from A to stack
2. push 2 from A to stack
3. pop 2 from stack to B
4. pop 1 from stack to B
5. push 3 from A to stack
6. pop 3 from stack to B

Example 2:

Input:
N = 3
A = {1,2,3}
B = {3,1,2}
Output:
0
Explanation:
Not Possible

You don't need to read input or print anything. Your task is to complete the function isStackPermutation() which takes two arrays A and as inputs and returns 1 if it is a stack permutation otherwise returns 0.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
1 <= N <= 105
1 <= A[i], B[i] <= 103
Sum of N over all test cases doesn't exceeds 106

### Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

#### My Submissions:  