Balanced Parentheses Checker
This challenge involves verifying if a given string of parentheses is "balanced." Balanced parentheses are essential in many programming contexts, such as parsing code, defining data structures, and mathematical expressions, to ensure correct nesting and matching of opening and closing symbols.
Problem Description
You need to implement a function that takes a string containing only the characters '(', ')', '{', '}', '[' and ']' as input. The function should determine if the parentheses in the string are correctly balanced.
A string of parentheses is considered balanced if:
- Every opening parenthesis has a corresponding closing parenthesis of the same type.
- The parentheses are closed in the correct order. For example,
([)]is not balanced because the square bracket is closed before the round parenthesis it encloses.
Key Requirements:
- The function must return
Trueif the string is balanced, andFalseotherwise. - The function should handle strings of varying lengths, including empty strings.
- Only the specified parenthesis characters should be considered. Other characters, if present in a more complex version, would be ignored. For this challenge, assume the input string only contains these six characters.
Expected Behavior:
- For a string like
"()"or"()[]{}", the function should returnTrue. - For a string like
"(]"or"([)]", the function should returnFalse. - For an empty string
"", the function should returnTrue.
Edge Cases to Consider:
- An empty string.
- A string with only opening parentheses (e.g.,
"((("). - A string with only closing parentheses (e.g.,
")))"). - A string where an opening parenthesis is closed by the wrong type of closing parenthesis (e.g.,
"(}"). - A string with nested parentheses (e.g.,
"({[]})").
Examples
Example 1:
Input: "()"
Output: True
Explanation: The round opening parenthesis is correctly closed by a round closing parenthesis.
Example 2:
Input: "()[]{}"
Output: True
Explanation: All opening parentheses have corresponding closing parentheses of the correct type and in the correct order.
Example 3:
Input: "(]"
Output: False
Explanation: The opening round parenthesis is closed by a square bracket, which is an incorrect match.
Example 4:
Input: "([)]"
Output: False
Explanation: The square bracket is opened inside the round parenthesis but closed outside of it, violating the order.
Example 5:
Input: "{[]}"
Output: True
Explanation: The curly brace encloses a correctly balanced set of square brackets.
Example 6:
Input: ""
Output: True
Explanation: An empty string is considered balanced as there are no parentheses to mismatch.
Constraints
- The input string
swill contain only the characters '(', ')', '{', '}', '[' and ']'. - The length of the input string
swill be between 0 and 10,000, inclusive. - Your solution should aim for an efficient time complexity, ideally O(n), where n is the length of the input string.
Notes
- Think about data structures that can help you keep track of opening parentheses as you encounter them.
- When you encounter a closing parenthesis, how can you quickly check if it matches the most recently opened, unclosed parenthesis?
- What should happen if you encounter a closing parenthesis but there are no open parentheses to match it?