import java.util.*;


public class Solution {

    public boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        if (pushV.length != popV.length) {
            return false;
        }
        int push = 0;
        for (int i = 0; i < popV.length; i++) {
            if (stack.size() == 0) {
                stack.push(pushV[push++]);
            }
            if (stack.peek() == popV[i]) {
                stack.pop();
            }else {
                while (stack.peek() != popV[i]) {
                    if (push == pushV.length) {
                        return false;
                    }
                    stack.push(pushV[push++]);
                }
                stack.pop();
            }
        }
        
        return true;
    }
}