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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
<strong>Output:</strong> true
<strong>Explanation:</strong> We might do the following sequence:
push(1), push(2), push(3), push(4),
pop() -&gt; 4,
push(5),
pop() -&gt; 5, pop() -&gt; 3, pop() -&gt; 2, pop() -&gt; 1
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> 1 cannot be popped before 2.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= pushed.length &lt;= 1000</code></li>
	<li><code>0 &lt;= pushed[i] &lt;= 1000</code></li>
	<li>All the elements of <code>pushed</code> are <strong>unique</strong>.</li>
	<li><code>popped.length == pushed.length</code></li>
	<li><code>popped</code> is a permutation of <code>pushed</code>.</li>
</ul>

class Solution:
    def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
        """
        This function validates whether the given sequence of pushed and popped operations can represent
        the behavior of a stack, where the sequence in 'pushed' is pushed onto the stack, and the sequence
        in 'popped' is the result of popping elements from the stack.

        Args:
        pushed (List[int]): The sequence of elements pushed onto the stack.
        popped (List[int]): The sequence of elements to be popped from the stack.

        Returns:
        bool: True if the 'popped' sequence can be obtained from the 'pushed' sequence by following stack
              push and pop operations. False otherwise.

        Time Complexity: O(n), where "n" is the length of the pushed or popped list. We iterate through each
                         element once.
        Space Complexity: O(n), where "n" is the length of the pushed or popped list, since we use a stack
                          to simulate the push and pop operations.
        """
        # Initialize an empty stack to simulate the stack operations.
        stack = []
        # Pointer 'index' to track the position in the popped list.
        index = 0

        # Iterate through each number in the pushed sequence.
        for num in pushed:
            # Simulate the push operation by appending the current number to the stack.
            stack.append(num)

            # After each push, check if the top of the stack matches the current element in the popped sequence.
            # If so, pop elements from the stack until it no longer matches the popped sequence or the stack is empty.
            while index < len(popped) and stack and popped[index] == stack[-1]:
                # Pop the top element from the stack as it matches the current element in popped.
                stack.pop()
                # Move to the next element in the popped sequence.
                index += 1

        # After processing all elements, if the stack is empty, it means the popped sequence is valid.
        # If the stack is not empty, it means the sequence is invalid.
        return not stack

# time complexity: O(n), where "n" is the length of the pushed or popped list.
# space complexity: O(n), where "n" is the length of the pushed or popped list.
16700