class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> stack = new Stack<Integer>();
for (int pushedIndex = 0, poppedIndex = 0; pushedIndex < pushed.length; pushedIndex++) {
stack.push(pushed[pushedIndex]);
while (!stack.isEmpty() && stack.peek() == popped[poppedIndex]) {
stack.pop();
poppedIndex++;
}
}
return stack.isEmpty();
}
}