
Problem Statement
This task revolves around simulating stack operations with two given integer arrays, pushed and popped. Both arrays consist of distinct integers. The challenge is to determine if the sequence represented by the popped array could result from a series of push and pop operations on an initially empty stack, using the order specified in the pushed array. If the sequence can be validly achieved through stack operations, the function should return true, otherwise false.
Examples
Example 1
Input:
pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
Output:
true
Explanation:
We might do the following sequence: push(1), push(2), push(3), push(4), pop() -> 4, push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Example 2
Input:
pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
Output:
false
Explanation:
1 cannot be popped before 2.
Constraints
1 <= pushed.length <= 10000 <= pushed[i] <= 1000- All the elements of
pushedare unique. popped.length == pushed.lengthpoppedis a permutation ofpushed.
Approach and Intuition
The solution to this problem relies on understanding the nature of stack operations—specifically, Last In First Out (LIFO). We can simulate the stack process and validate if the popped sequence is feasible based on the pushed sequence.
Observations:
- Since the elements are unique and
poppedis a permutation ofpushed, every element should appear once in both arrays. - The key to the solution is to replicate the push and pop sequence and see if we can match the
poppedarray.
Key Steps:
- Initialize an empty stack.
- Traverse through each element in the
pushedarray:- Push elements from
pushedonto the stack. - After each push operation, check if the top element matches the next needed pop (the current element in
popped). If it does, pop it from the stack. Continue this check in a loop (since consecutive pops might be in order) until the stack's top doesn't matchpoppedor the stack is empty.
- Push elements from
- If after processing all elements, the stack is empty, then return
true, signaling that it's possible to achieve thepoppedsequence. If not, returnfalse.
Example Analysis:
Using Example 1 from the problem statement:
pushed = [1,2,3,4,5],popped = [4,5,3,2,1]- Simulate operations:
- Push
[1,2,3,4]. When4is on top of the stack, and4is next inpopped, pop it. Continue with similar operations for all elements. - Result: Stack operations match the
poppedsequence, so returntrue.
- Push
In Example 2, however:
pushed = [1,2,3,4,5],popped = [4,3,5,1,2]- The sequence
1, 2is problematic. As1is pushed before2,2should be popped before1, making this sequence unachievable by standard stack operations. Thus, the result isfalse.
Through accurately replicating the push and pop operations and constantly comparing with the popped sequence, the function can verify the feasibility of popped being a valid stack sequence derived from pushed.
Solutions
- Java
class Solution {
public boolean isValidSequence(int[] pushed, int[] popped) {
int len = pushed.length;
Stack<Integer> tempStack = new Stack<>();
int counter = 0;
for (int value : pushed) {
tempStack.push(value);
while (!tempStack.isEmpty() && counter < len && tempStack.peek() == popped[counter]) {
tempStack.pop();
counter++;
}
}
return counter == len;
}
}
This article guides you through validating stack sequences in Java. The provided solution uses a single Stack to simulate the pushing and popping operations based on the given sequences.
- Start by initializing a
Stackto hold thepushedvalues during simulation. - Use a
counterto track the index of the next expectedpopoperation.
For each value in the pushed array:
- Push the value onto the stack.
- Continuously check the condition: while the stack is not empty, and the top value of the stack matches the current
poppedvalue indicated bycounter. - If conditions above are met, pop the top value off the stack and increment
counter.
- By the end of iteration through
pushedvalues, ensure that allpoppedvalues are correctly matched by checking ifcounterequals the length of thepushedorpoppedarray, returning true or false accordingly.
This approach effectively simulates the actual push and pop operations as defined in the problem, determining if a given stack push and pop sequence is feasible.