> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Validate Stack Sequences Python Solution

> Tested Python solution for LeetCode 946 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 946, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Stack](/catalog/topics/stack), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/validate-stack-sequences/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 946   # by problem number
lcpy gen -s validate_stack_sequences   # by problem name
```

## Problem

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

### Examples

```
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
```

```
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.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/validate_stack_sequences/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/validate_stack_sequences/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def validate_stack_sequences(self, pushed: list[int], popped: list[int]) -> bool:
        stack: list[int] = []
        pop_index = 0
        for value in pushed:
            stack.append(value)
            while stack and stack[-1] == popped[pop_index]:
                stack.pop()
                pop_index += 1
        return pop_index == len(popped)
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.