Balanced Brackets Problem in Data Structures in C++

Last Updated :

Write a program to validate whether the brackets in the given input string are balanced or not.

Examples:

Input : {[()]} 
Output : Valid

Input :{{(}}[]
Output : Invalid

Explanation:

This problem is a very common application of stacks in data structures. An opening bracket may be ‘{‘,'[‘,'(‘ and their corresponding closing brackets are ‘}’,’]’,’)’. The brackets are said to be balanced if and only if for each opening bracket there is exactly one closing bracket, without any incomplete pair of brackets.

This problem can easily using stack implementation. Stack is a data structure that follows the Last In First Out Concept. Push the upcoming opening brackets in the stack, if any closing bracket occurs then check in stack if it’s corresponding opening bracket occurs. If it exists then pop the opening bracket.

If the value of top is greater than 0 then it is invalid.

C++ code implementation :

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	char stack[100];
	int top=-1,j,flag=0;
	string s;
	getline(cin,s);
	int i=s.size();
	for(j=0;j<i;j++)
	{
		if(s[j]=='{' || s[j]=='(' || s[j]=='[')
		{
			top++;
			stack[top]=s[j];
		}
		if(s[j]=='}' || s[j]==']' || s[j]==')')
		{
			if(s[j]==')' && stack[top]=='(')
			{
				top--;
			}
			if(s[j]=='}' && stack[top]=='{')
			{
				top--;
			}
			if(s[j]==']' && stack[top]=='[')
			{
				top--;
			} 
		}
	}
	if(top<0)
	cout<<"Valid";
	else
	cout<<"Invalid";

return 0;
} 

Output:

Input : {[()]} 
Output : Valid

Input :{{(}}[]
Output : Invalid

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Comment
Next Article
Mithlesh Upadhyay Published 06 Feb, 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Similar Reads

  • Compare Two Lists

    In dart programming, Lists are used for the collection of data. We declare just variables instead of declaring a separate variable for each value of the collection. Lists…

    3 min read
  • Find the Nth Triacontakaihenagonal Number

    Given a number N, the task is to find Nth triacontakaihenagonal number. A triacontakaihenagonal number is class of figurate number. It has 31 – sided polygon called triacontakaihenagon.…

    1 min read
  • Check if a Number is a Tetracontagon Number

    Given an integer N, the task is to check if it is a Tetracontagon number or not. Tetracontagon number is class of figurate number. It has 40 –…

    2 min read
  • Find max rational number in given Array

    Given the Numerator and Denominator of N Rational Numbers, the task is to find the maximum Rational Number from the array. Examples: Input: N = 2 num[] =…

    2 min read
  • Check if a Number is Pentacontagon

    Given an integer N, the task is to check if it is a pentacontagon number or not. Pentacontagon number is class of figurate number. It has 50 –…

    2 min read
  • Check if a Number is Triacontakaidigon

    Given an integer N, the task is to check if it is a Triacontakaidigon number or not. Triacontakaidigon number is class of figurate number. It has 32 –…

    2 min read