Tuesday 21 March 2023

Check for Balanced Brackets in an expression (well-formedness) Input: exp = “[()]{}{[()()]()}” in Java

Introduction:

In computer science, balanced parentheses are important to check the syntax of an expression. An expression is considered balanced when every opening parenthesis has a corresponding closing parenthesis, and they are in the correct order. In this blog post, we will discuss how to check for balanced brackets in an expression in Java.


Algorithm:

To check for balanced brackets in an expression, we can use a stack data structure. We will traverse the expression from left to right and push every opening bracket onto the stack. When we encounter a closing bracket, we will pop the topmost element from the stack and compare it with the closing bracket. If the brackets match, we continue with the traversal; otherwise, we return false, indicating that the expression is not balanced. After completing the traversal, if the stack is empty, we return true; otherwise, we return false.


Let's look at the code for the same.


Java Code:



import java.util.*;


public class BalancedBrackets {

   public static boolean isBalanced(String expression) {

      Stack<Character> stack = new Stack<Character>();

      for (int i = 0; i < expression.length(); i++) {

         char ch = expression.charAt(i);

         if (ch == '(' || ch == '{' || ch == '[') {

            stack.push(ch);

         } else if (ch == ')' || ch == '}' || ch == ']') {

            if (stack.isEmpty()) {

               return false;

            } else if (ch == ')' && stack.peek() == '(') {

               stack.pop();

            } else if (ch == '}' && stack.peek() == '{') {

               stack.pop();

            } else if (ch == ']' && stack.peek() == '[') {

               stack.pop();

            } else {

               return false;

            }

         }

      }

      return stack.isEmpty();

   }


   public static void main(String[] args) {

      String expression = "[()]{}{[()()]()}";

      if (isBalanced(expression)) {

         System.out.println("The expression is balanced.");

      } else {

         System.out.println("The expression is not balanced.");

      }

   }

}

Explanation:

In this code, we have defined a method named isBalanced, which takes a String expression as input and returns a boolean value indicating whether the expression is balanced or not.


We have used a stack data structure to keep track of the opening brackets. We traverse the expression from left to right and push every opening bracket onto the stack. When we encounter a closing bracket, we pop the topmost element from the stack and compare it with the closing bracket. If the brackets match, we continue with the traversal; otherwise, we return false, indicating that the expression is not balanced. After completing the traversal, if the stack is empty, we return true; otherwise, we return false.


In the main method, we have created a String expression and called the isBalanced method to check whether the expression is balanced or not.


Conclusion:

In this blog post, we discussed how to check for balanced brackets in an expression in Java. We used a stack data structure to keep track of the opening brackets and compared every closing bracket with the topmost element of the stack. If the brackets matched, we continued with the traversal; otherwise, we returned false. Finally, we checked whether the stack was empty or not to determine whether the expression was balanced or not.

1 comment: