HomeEducationWhat is Balanced Parenthesis?

What is Balanced Parenthesis?

“Check the balanced parentheses” 

It is one of the most popular and commonly asked questions in a coding or a programming interview.

Because of the due weightage given to this concept, the concept is highly beneficial to be learned by every coder or the programmer. 

Balanced parentheses is defined as the opening of brackets or closing of brackets by maintaining logical sequence. 

Here, in this problem you are given with certain string expressions in an adequate format which you have to find as per the characters. 

Do you want to know about this problem in-depth?

If yes, you are on the right blog post!

Learn about balanced parentheses in detail and fill your knowledge bank.

Balanced Parentheses 

Let’s understand this term with the help of an example:

We are moving our attention towards the stack problem in computer science. Here an arithmetic expression can be written as:

[ 5 + 6 ] * [ 7 + 8 ] / [ 4 +3 ] 

Here in this case the parentheses are useful in order to perform multiple operations. You may be having some sort of experience in the given language where you can construct the parentheses. 

So balanced parentheses is defined as the opening of the symbols with the certain corresponding symbols each having the separate pair of properly nested parentheses.

How to Solve the Problem of  Balanced Parentheses?

The problem of balanced parentheses can be easily solved with the help of a stack. The goal will be to stack all of the opening brackets. When you hit any of the closing brackets of the same type it may reach at the top of your stack. 

If the statement turns out to be true, you can pop out the stack and carry it with the required iteration. Though, if the stack is shown to be empty at the end of each bracket, all of the brackets will be correctly created. Though, it is necessary to check if it is balanced or not. 

For that, you can either check it with the help of the problem statement. 

Consider the problem statement 

An input expression of a string with length n is given to you. t=it consists of basically three parentheses {,}, (,), [, ] 

Parentheses are defined as balanced only if:

  • To close all the open ones, same type of parentheses is being used
  • Here the proper sequencing must be used in order to close the open parentheses 

In this example; there will be a closing bracket in correspondence to their opening brackets. This will be a kind of  balanced parentheses.

Though, in the second one it will be observational that the parentheses will not be balanced [ as the 2nd position in your expression can be closed before its end]

Though, there is no { for the given }. Due to this condition, an error may occur which may make it difficult for you to run the code. 

Approaches in  Balanced Parentheses

To solve the problem of  balanced parentheses, there are various ways that you can follow: 

One easy to use approach that is most commonly used in such a case would be to use the stack-based approach which will be having the space complexity O [ n] alongwith the time complexity O N]. But here, no extra space will be used:

Pointer Based Approach in the Case of  Balanced Parentheses

In this approach, we will first maintain the top pointer in order to use your parentheses in an ideal way. We will first point to the last occurring open bracket so as to traverse the whole string.

Steps in this case to be followed are:

  • Initialise the top as the -1 
  • Then start your iteration from the given expression 
  • If your top <o of the current character with an open bracket at its top index, you can increment the top ++ [ as most currently used bracket that is occurring. This will mainly be the open bracket]
  • Otherwise, if the given current character will not be matching with your open bracket at the top of your index, it can decrease the top point to the 1 [ it will keep on pointing at the open bracket that has not yet matched]
  • After iterating all over with the characters, if your first pointer will get back to the -1, all open brackets will be matched with the closing brackets in a proper order [ you may in this  case find the accurate  balanced parentheses]

Complexity analysis 

In this case the time complexity would be of the algorithm O [n] while its space complexity would be O [1] with not any kind of additional spaces. 

Check with another example;

“ {[(}])}”

This is the unbalanced input as the pair of given round brackets () are enclosed in the unbalanced closing square brackets ], with the pair of given square brackets [] which gets enclosed in your single unbalanced opening brackets “)”

The string with the brackets is said to be in the blanched position if it is satisfying the following conditions:

  • The matching closing bracket will occur if it is to the right of your each opening bracket that are corresponding to each other 
  • If the brackets are enclosed within the blanched brackets they also needs to be balanced
  • It will not contain any kind of the non-character character 

Algorithm would be:

  • First declare the character stack 
  • Convert all input string into the character array
  • Traverse input string by pushing current character with the starting bracket. Pop the current character of the stack with its closing bracket. If pop character is not matching the starting bracket, brackets are not balanced

Alpha Beta Pruning in Artificial Intelligence

Another essential concept that every programmer should know is Alpha beta pruning in artificial intelligence. It is defined as the optimization technique for the given minimax algorithm that aids in decreasing the number of nodes in a particular search tree.

Wrapping up 

Balanced parentheses is touted as an essential topic that can be asked in almost every technical coding interview. 

With this guide, learn about the technicalities of this key concept and implement it in a better way.

Happy learning!

Must Read