> ## Content Index
> Fetch the complete content index at: https://andybase.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# 946 Validate Stack Sequences
- URL: https://andybase.com/946-validate-stack-sequences/
- Published: 2018-11-26T19:33:29.000Z
- Updated: 2018-11-26T19:33:29.000Z
- Author: andy
- Tags: leetcode, contest, #Migrated-1784083136856, #wp, #wp-post, #Import 2026-07-15 02:38

Link: [validate stack sequences](https://leetcode.com/problems/validate-stack-sequences/?ref=andybase.com)

## Thought

Its pretty straight forward, just stimulate a stack and push/pop as the sequence shows. 

## Code

```
class Solution(object):
    def validateStackSequences(self, pushed, popped):
        """
        :type pushed: List[int]
        :type popped: List[int]
        :rtype: bool
        """
        stack = []
        visited = set()
        N = len(popped)
        j = 0
        for i in pushed:
            stack.append(i)
            while stack and stack[-1] == popped[j] and j < N:
                stack.pop()
                j += 1
        if stack:
            return False
        return True
```

## Runtime

Time: O(n)

Space: O(n)