Balanced Paranthesis

Stack / Queues Implementation Easy
  • This problem is essential in software development, particularly in languages like Python, JavaScript, or C++ where brackets are used extensively to delimit blocks of code, arrays and objects
  • An imbalance in brackets can cause a program to fail, often leading to "syntax error" messages
  • This real-world application is not exclusive to coding; it's also vital in validation of data formats like JSON and XML, ensuring that they are well-structured and correctly nested

Given string str containing just the characters '(', ')', '{', '}', '[' and ']', check if the input string is valid and return true if the string is balanced otherwise return false.

Examples:

Input: str = “()[{}()]”

Output: True

Explanation: As every open bracket has its corresponding close bracket. Match parentheses are in correct order hence they are balanced.

Input: str = “[()”

Output: False

Explanation: As ‘[‘ does not have ‘]’ hence it is not valid and will return false.

Input: str = "{[()]}"

Constraints

1 <= str.length <= 104

str consists of parentheses only '()[]{}'.

Hints

  • Use a stack to store opening brackets (, {, [.
  • When encountering a closing bracket ), }, ]. Check if the top of the stack is its corresponding opening bracket. If matched, pop the opening bracket. Otherwise, return false.

Company Tags

Goldman Sachs Walmart Twilio Rockstar Games Red Hat Cerner Oracle PwC GE Healthcare Johnson & Johnson Docker Zomato Electronic Arts Western Digital HCL Technologies Siemens Healthineers Ubisoft ARM Micron Technology Uber Deloitte Broadcom Snowflake Stripe Robinhood Google Microsoft Amazon Meta Apple Netflix Adobe TCS Cognizant Accenture Infosys Capgemini Wipro