easyStackPattern: Stack

Validate Stack Sequence Solution

Problem Statement

You are given a sequence of integers where 27 represents a push operation and 91 represents a pop operation. Implement a function to validate if the given sequence is properly nested using a stack data structure.

Examples

Example 1:
Input:[27, 27, 91, 91]
Output:true
Explanation: Properly nested sequence of push and pop operations
Example 2:
Input:[27, 91, 27, 91]
Output:true
Explanation: Properly nested sequence of push and pop operations
Example 3:
Input:[27, 27, 91]
Output:false
Explanation: More push operations than pop operations

Constraints

  • The input string can be arbitrarily long.
  • The input string can contain any character, but only the parentheses ('()', '[]', '{}') are relevant for determining validity.
  • The parentheses can be nested to any depth.
Time: O(n) Space: O(n)
Use a stack to keep track of opening commands and iterate over the sequence of commands in O(n) time complexity

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.