946. Validate Stack Sequences LeetCode Solution

In this guide, you will get 946. Validate Stack Sequences LeetCode Solution with the best time and space complexity. The solution to Validate Stack Sequences problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Validate Stack Sequences solution in C++
  4. Validate Stack Sequences solution in Java
  5. Validate Stack Sequences solution in Python
  6. Additional Resources
946. Validate Stack Sequences LeetCode Solution image

Problem Statement of Validate Stack Sequences

Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise.

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 <= 1000
0 <= pushed[i] <= 1000
All the elements of pushed are unique.
popped.length == pushed.length
popped is a permutation of pushed.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

946. Validate Stack Sequences LeetCode Solution in C++

class Solution {
 public:
  bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
    stack<int> stack;
    int i = 0;  // popped's index

    for (const int x : pushed) {
      stack.push(x);
      while (!stack.empty() && stack.top() == popped[i]) {
        stack.pop();
        ++i;
      }
    }

    return stack.empty();
  }
};
/* code provided by PROGIEZ */

946. Validate Stack Sequences LeetCode Solution in Java

class Solution {
  public boolean validateStackSequences(int[] pushed, int[] popped) {
    Deque<Integer> stack = new ArrayDeque<>();
    int i = 0; // popped's index

    for (final int x : pushed) {
      stack.push(x);
      while (!stack.isEmpty() && stack.peek() == popped[i]) {
        stack.pop();
        ++i;
      }
    }

    return stack.isEmpty();
  }
}
// code provided by PROGIEZ

946. Validate Stack Sequences LeetCode Solution in Python

class Solution:
  def validateStackSequences(
      self,
      pushed: list[int],
      popped: list[int],
  ) -> bool:
    stack = []
    i = 0  # popped's index

    for x in pushed:
      stack.append(x)
      while stack and stack[-1] == popped[i]:
        stack.pop()
        i += 1

    return not stack
# code by PROGIEZ

Additional Resources

See also  1105. Filling Bookcase Shelves LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.