mediumStackPattern: Implementing a Stack to Validate Sequences
Bracket Sequence Validator Solution
Problem Statement
Given a string of bracket characters, implement a function to determine if the sequence is balanced. A sequence is considered balanced if every opening bracket has a corresponding closing bracket of the correct type and the brackets are properly nested.
Examples
Example 1:
Input:()
Output:true
Explanation: The sequence has a matching opening and closing bracket
Example 2:
Input:)(
Output:false
Explanation: The sequence does not have matching opening and closing brackets
Example 3:
Input:(())
Output:true
Explanation: The sequence has properly nested brackets
Constraints
- The input sequence will contain at most 1000 HTML tags.
- The input sequence will only contain the following HTML tags: <html>, <body>, <h1>, <p>.
Time: O(n) Space: O(n)
An optimized approach would use a stack to keep track of the opening tags, allowing for a time complexity of O(n) and a space complexity of O(n).
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
