Balanced Parenthesis Problem in C++

Understanding Balanced Parenthesis Problem

The balanced parenthesis problem involves checking whether a given string of brackets is balanced or not.

We will explore three different methods to solve this problem in C++.

Method 1: Using Stack

#include <iostream>
#include <stack>
using namespace std;

bool isBalanced(string expr) {
    stack s;
    for (char ch : expr) {
        if (ch == '(' || ch == '{' || ch == '[') {
            s.push(ch);
        } else {
            if (s.empty()) return false;
            char last = s.top(); s.pop();
            if ((ch == ')' && last != '(') || 
                (ch == '}' && last != '{') || 
                (ch == ']' && last != '[')) {
                return false;
            }
        }
    }
    return s.empty();
}

int main() {
    string expr = "{[()]}";
    cout << (isBalanced(expr) ? "Balanced" : "Not Balanced") << endl;
    return 0;
}
            

Method 2: Using Counter

#include <iostream>
using namespace std;

bool isBalanced(string expr) {
    int count = 0;
    for (char ch : expr) {
        if (ch == '(') count++;
        else if (ch == ')') count--;
        if (count < 0) return false;
    }
    return count == 0;
}

int main() {
    string expr = "(())";
    cout << (isBalanced(expr) ? "Balanced" : "Not Balanced") << endl;
    return 0;
}
            

Method 3: Recursion

#include <iostream>
using namespace std;

bool checkBalance(string expr, int index, int count) {
    if (count < 0) return false;
    if (index == expr.size()) return count == 0;
    if (expr[index] == '(') return checkBalance(expr, index + 1, count + 1);
    if (expr[index] == ')') return checkBalance(expr, index + 1, count - 1);
    return checkBalance(expr, index + 1, count);
}

int main() {
    string expr = "((()))";
    cout << (checkBalance(expr, 0, 0) ? "Balanced" : "Not Balanced") << endl;
    return 0;
}
            
Top 100 Codes By Learn-for-free
Start Preparing Arraysform here👇

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

Find Largest Element in an Array: C C++ Java Python

Find Smallest Element in an Array: C C++ Java Python

Find the Smallest and Largest Element in an Array: C C++ Java Python

Find Second Smallest Element in an Array: C C++ Java Python

Calculate the Sum of Elements in an Array: C C++ Java Python

Reverse an Array: C C++ Java Python

Sort First Half in Ascending Order and Second Half in Descending: C C++ Java Python

Finding the Frequency of Elements in an Array: C C++ Java Python

Counting the Number of Even and Odd Elements in an Array: C C++ Java Python

Finding Maximum Product Sub-array in a Given Array: C C++ Java Python

Finding Arrays are Disjoint or Not: C C++ Java Python

Finding Equilibrium Index of an Array: C C++ Java Python

Rotation of Elements of Array - Left and Right: C C++ Java Python

Balanced Parenthesis Problem: C C++ Java Python