BackmediumStack PhonePe

Balanced Bracket Validator Solution

Problem Statement

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

Example 1
Input
{}[](){}[]()
Output
true

Explanation: Step-by-step: We start with an empty stack. We iterate over the string from left to right. When we encounter an opening bracket, we push it onto the stack. When we encounter a closing bracket, we check if the top of the stack contains the corresponding opening bracket. If it does, we pop the opening bracket from the stack. If it doesn't, we return False. After iterating over the entire string, if the stack is empty, we return True; otherwise, we return False.

Example 2
Input
{[()]}
Output
true

Explanation: Step-by-step: We start with an empty stack. We iterate over the string from left to right. When we encounter an opening bracket, we push it onto the stack. When we encounter a closing bracket, we check if the top of the stack contains the corresponding opening bracket. If it does, we pop the opening bracket from the stack. After iterating over the entire string, if the stack is empty, we return True; otherwise, we return 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 Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free