BackmediumStackZomato

Bracket Sequence Validator Solution

Problem Statement

Given a string of bracket characters, implement a function to determine if the sequence is balanced.

Example 1
Input
{[]}
Output
false

Explanation: Step-by-step: The input '{[]}' does not contain any HTML tags. We start by checking the opening brackets. We have '{' which is an opening bracket. Then we have '[' which is also an opening bracket. However, we do not have any corresponding closing brackets for these opening brackets. Therefore, the sequence is not balanced and the output is false.

Example 2
Input
({[]})
Output
false

Explanation: Step-by-step: The input '({[]})' does not contain any HTML tags. We start by checking the opening brackets. We have '(' which is an opening bracket. Then we have '{' which is also an opening bracket. Then we have '[' which is an opening bracket. Then we have '}' which is a closing bracket for '{'. Then we have ']' which is a closing bracket for '['. Finally, we have ')' which is a closing bracket for '('. Therefore, the sequence is balanced and the output is true. However, the problem statement does not handle HTML tags and the output should be false.

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>.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Bracket Sequence Validator — Problem Statement & Solution Guide

StackMediumImplementing a Stack to Validate Sequences
TimeO(n)
|
SpaceO(n)

Problem Description

Given a string of bracket characters, implement a function to determine if the sequence is balanced.

Examples

Example 1

Input

{[]}

Output

false

Explanation: Step-by-step: The input '{[]}' does not contain any HTML tags. We start by checking the opening brackets. We have '{' which is an opening bracket. Then we have '[' which is also an opening bracket. However, we do not have any corresponding closing brackets for these opening brackets. Therefore, the sequence is not balanced and the output is false.

Example 2

Input

({[]})

Output

false

Explanation: Step-by-step: The input '({[]})' does not contain any HTML tags. We start by checking the opening brackets. We have '(' which is an opening bracket. Then we have '{' which is also an opening bracket. Then we have '[' which is an opening bracket. Then we have '}' which is a closing bracket for '{'. Then we have ']' which is a closing bracket for '['. Finally, we have ')' which is a closing bracket for '('. Therefore, the sequence is balanced and the output is true. However, the problem statement does not handle HTML tags and the output should be false.

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

Optimal Approach & Strategy

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

Brute Force Approach

A brute-force approach would involve checking every possible subsequence of the input to see if it is valid, resulting in a time complexity of O(n²). This approach is inefficient and would not be suitable for large inputs.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function isBalanced(s) { let stack = []; let bracketMap = {')': '(', '}': '{', ']': '['}; for (let i = 0; i < s.length; i++) { if (s[i] === '(' || s[i] === '{' || s[i] === '[') { stack.push(s[i]); } else if (s[i] === ')' || s[i] === '}' || s[i] === ']') { if (stack.length === 0 || bracketMap[s[i]] !== stack.pop()) { return false; } } } return stack.length === 0; }

Asked in Top Tech Interviews

Zomato

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.