Check Pair of Brackets (Well-Formatted) in an Expression

Check Pair of Brackets (Well-Formatted) in an Expression

A simple program to check the pair of brackets using Stack in Java, Python, and C#.

A simple program to check the pair of brackets using Stack in Java, Python, and C#.

This is a most favorite question for interviewers, so prepare it before appearing for the interview.

Problem: Check Bracket Pairs/Balanced parenthesis using Stack. Check out the below expression, write a program to check whether started bracket ends or not. Order of Brackets like, ‘[’,’]’,‘{’,’}’, ‘(’,’)’ must be correct, then print “Paired”; otherwise, “Not Paired”. Input: [({})] Output: Paired Input: [(){] Output: Not Paired

Code for Java using Stack

```// Easy Java program for Bracket Pairs checking import java.util.*;

public class BracketPairsChecker {

// Method to check if brackets have complete pair or not.
public static boolean areBracketsPaired(String expr)
    // Using ArrayDeque is faster than using Stack class
    Deque<Character> stack
        = new ArrayDeque<Character>();

    // Traversing the Expression
    for (int i = 0; i < expr.length(); i++)
        char x = expr.charAt(i);

        if (x == '(' || x == '[' || x == '{')
            // Push the element in the stack

        // If current character is not opening bracket, then it must be closing. So stack cannot be empty at this point.
        if (stack.isEmpty()) {
            return false;
        char check;
        switch (x) {
        case ')':
            check = stack.pop();
            if (check == '{' || check == '[')
                return false;

        case '}':
            check = stack.pop();
            if (check == '(' || check == '[')
                return false;

        case ']':
            check = stack.pop();
            if (check == '(' || check == '{')
                return false;

    // Check Empty Stack
    return (stack.isEmpty());

// Main method to check the code.
public static void main(String[] args)
    String expr = "[({})]";
    // String expr = "[(){]";

    // Function call
    if (areBracketsPaired(expr))
        System.out.println("Not Paired");


Read more program made in C#, Python, Java and C langauage.

Article originally published on Medium.

python java programming developer web-development csharp

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Python Developer | Python web development company india

Hire Python Developer from us for Scalable, Secure & Robust Python Web development Solutions. Strict NDA | 16+ Years Exp| 2500+ Clients| 450+ Experts

Hire Dedicated Java Developer and Offshore Java Developers

For world-class java web application development services you must hire dedicated java developer from a reputed software development company – ByteCipher Pvt. Ltd

Top Python Web Development Companies That Deliver Staggering Assistance To Enterprises

You must have wondered so many times which programming language is capable of deploying modern technology concepts and bringing unthinkable systems into digital reality. Well! The answer to your quest is Python.  It is an incredible potential programming language winning the IT marketplace with its superpower to create solutions for scientific researches, the healthcare system, and almost every industry needs. It

Hire Java Developers | Java Web Application Development Company | Java development company

Hire Java developers diversified as Spring developers, Vaadin developers, GWT developers. We offer Java developers for hire on per hour rate.

Hire Expert Python Developers | Hire Top Python Developers

For your custom Python development projects, hire our dedicated Python Developers on an hourly/full-time basis. We are experts in working with latest python frameworks.