Javatpoint Logo
Javatpoint Logo

CHECK FOR BALANCED BRACKETS IN AN EXPRESSION (WELL FORMEDNESS)

Identifying the issue:

Your objective is to determine whether an expression has balanced brackets or not, given an expression with distinct open and closing brackets. The following parentheses can be used in the expression: '(', ')', '', '', '[', ']'. If the expression is balanced, it means:

  • Both the opening and closing brackets are in the same position.
  • Close the opening bracket with the exact same type of closing bracket.

For instance

Input: "[(())]"
output : True.

Input expression="[(])"

Output: False.

Possible follow-up questions

Is it feasible for the phrase to include characters other than brackets? (Answer: For the sake of simplicity, assume no other characters are there.)

Solutions

Based on several perspectives, we will discuss the various possible solutions to this question. The following are the recommended solutions:

Brute force: We shall keep a counter for the brute force solution. For balanced expressions, the value of this counter should never be less than zero.

Time-Optimized Brute Force Method We will also use a counter in this method, but we will replace the matched pairs with *.

Solution Using Stack in this solution, we will use stack ideas to determine whether the phrase is balanced or not.

Brute Force

Solution Idea:

The aim is to keep a counter that is incremented when an opening bracket occurs and decremented when a closing bracket is met. If the value of the count is less than zero at any point in time, the expression isn't balanced. If the closing bracket is at index j for every opening bracket at index i, we must ensure that any other pair of closing and opening brackets are strictly in between the initial matching pair. (As in the second case in the problem description.)

Step one of the solution

For each opening bracket, we will look for the presence of a closing bracket within the permissible range, that is, after the opening bracket but before the phrase ends.

Similarly, for each closing bracket, we will check for the presence of the opening parenthesis from the beginning of the expression to the index of the closing bracket.

When we find one matching pair, we'll seek out more brackets in between and see whether they're within an acceptable range.

Pseudo-Code



Analysis of Complexity

Time Complexity: O(N3), where N is the expression size.

Space Complexity: O(1)

Consider these critical thoughts!

Can you analyses the temporal complexity of the preceding code by running it dry?

Time-Optimized Brute Force Solution

Concept for a Solution

Because the above approach has a high time complexity, we shall create a different solution to the problem. We'll keep the counter variable and increment and decrement it when we meet the opening and closing brackets. However, once we find the matched pair, we will substitute it with '*'. This will allow us to avoid having to care for those matched pairs. Finally, we will check the current value of the counter; if it is zero, the expression is balanced.

Steps to Resolve

For each closing bracket, we shall look for the opening bracket from the previous opening bracket to the index of the current closing bracket.

When the opening and closing brackets match, we will replace the pair with a '*'.
Finally, we will examine the value of counter; if it is zero, our expression appears balanced.

Pseudo-Code


Complexity Analyses

Time Complexity: O(N2) (Think!)

Space Complexity: O(1) Are you considering a data structure that could easily handle this problem?

Solution Using Stack

Concept for a Solution

To evaluate an expression, we are going to employ a stack data structure in this solution. Stack is a LIFO-based data structure with a single entry and exit point. You may read more about it here: The stack and its core actions When we come across an opening bracket, we will add it to the stack, and when we come across a closing bracket, we will contrast it with the top element. If the top element is not part of the corresponding opening bracket, we are going to return false; otherwise, we will pop the corresponding top opening bracket. For example, the supplied expression is "[()]."

Solution Procedures

If we find an opening bracket, we'll just push it to the stack.
If we get a closing bracket, we'll compare the top element.
We can return false if there is no match between the closing and opening brackets.
Otherwise, we will pop the top element and continue.
Finally, if the stack is empty, we can conclude the expression is balanced.

Pseudo-Code

Complexity Analysis

Time Complexity: O(N)(consider)

Space Complexity O(N) (Stack Space)

Conclusion:

Finally, utilizing stack to check for balanced brackets in an expression is an important idea in computer science. A balanced expression makes sure the programme can run without mistakes, making it an important stage in many applications like compilers, parsers, and data validation.

Overall, knowing how to use a stack to check for balanced brackets in C programming is a necessary skill for programmers and computer science students. It serves as the foundation for many sophisticated computer science principles and is employed in a variety of real-world applications.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA