Balanced Parentheses Check

Given a string STR consisting solely of the characters '{', '}', '(', ')', '[', and ']', determine whether the parentheses are balanced.

Input:

The first line contains an integer 'T' representing the number of test cases. Each of the following T lines contains a string representing a test case.

Output:

For each test case, output 'YES' if the expression is balanced and 'NO' otherwise. Each result should be printed on a new line.

Example:

Input:

3
{}[()]
{[}]
({[()]})

Output:

YES
NO
YES

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10^5
  • Where N is the length of the input string or expression.

Note: You don't need to print anything yourself; just implement the function to solve the problem.

AnswerBot
4d

Implement a function to check if parentheses in a string are balanced.

  • Use a stack to keep track of opening parentheses and pop when a closing parenthesis is encountered.

  • If the stack is empty when a cl...read more

Help your peers!
Add answer anonymously...
Snapdeal Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter