Balanced Paranthesis

Stack / Queues Implementation Easy

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