Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Balanced Parentheses Checker

A simple C++ DSA project that checks whether the brackets in a given expression are balanced using the Stack data structure.

Features

  • Checks (), {}, and []
  • Supports nested brackets
  • Supports mathematical expressions containing variables and operators
  • Detects mismatched brackets
  • Uses Stack for efficient bracket matching

Examples

Input: {[()]}

Output: Expression is BALANCED

Input: ([)]

Output: Expression is NOT BALANCED

Input: a+(b*c)

Output: Expression is BALANCED

Data Structure Used

Stack

Opening brackets are pushed into the stack.

When a closing bracket is encountered, it is compared with the bracket at the top of the stack.

If they match, the opening bracket is removed using pop().

At the end, if the stack is empty, the expression is balanced.

Algorithm

  1. Read the expression.
  2. Traverse each character.
  3. Push opening brackets into the stack.
  4. For a closing bracket, check the top of the stack.
  5. If the brackets match, pop the stack.
  6. If they do not match, the expression is not balanced.
  7. If the stack is empty after traversal, the expression is balanced.

Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(n)

Technologies Used

  • C++
  • STL Stack
  • String

How to Run

Compile:

g++ main.cpp -o checker

Run:

./checker

About

A C++ DSA project to check balanced parentheses using Stack.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages