import java.util.ArrayList;
import java.util.*;

public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {

        if (pushA.length == 1 && popA.length == 1) {
            return pushA[0] == popA[0];
        }

        int p1 = 0;
        int p2 = 0;
        Stack<Integer> stack = new Stack<>();

        while (p1 != pushA.length) {
            if (stack.isEmpty()) {
                stack.push(pushA[p1]);
                p1++;
            }
            else {
                int top = stack.peek();
                if (top == popA[p2]) {
                    p2++;
                    stack.pop();
                }
                else {
                    stack.push(pushA[p1]);
                    p1++;
                }
            }
        }
        while (!stack.isEmpty() && stack.peek() == popA[p2]) {
            stack.pop();
            p2++;
        }
        if (stack.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
}