
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Balance a string after removing extra brackets in C++
A string is an array of characters. In this problem, we are given a string which has opening and closing brackets. And we will balance this string by removing extra brackets from the string.
Let’s take an example,
Input : “)Tutor)ials(p(oin)t(...)” Output : “Tutorials(p(oin)t(...))”
To solve this problem, we will traverse through the string and check for matching brackets. For unmatched brackets eliminate the closing brackets.
Algorithm
Step 1 : Traverse the string from left to right. Step 2 : For opening bracket ‘(’ , print it and increase the count. Step 3 : For occurence of closing bracket ‘)’ , print it only if count is greater than 0 and decrease the count. Step 4 : Print all characters other than brackets are to be printed in the array. Step 5 : In the last add closing brackets ‘)’ , to make the count 0 by decrementing count with every bracket.
Example
#include<iostream> #include<string.h> using namespace std; void balancedbrackets(string str){ int count = 0, i; int n = str.length(); for (i = 0; i < n; i++) { if (str[i] == '(') { cout << str[i]; count++; } else if (str[i] == ')' && count != 0) { cout << str[i]; count--; } else if (str[i] != ')') cout << str[i]; } if (count != 0) for (i = 0; i < count; i++) cout << ")"; } int main() { string str = ")Tutor)ials(p(oin)t(...)"; cout<<"Original string : "<<str; cout<<"\nBalanced string : "; balancedbrackets(str); return 0; }
Output
Original string : )Tutor)ials(p(oin)t(...) Balanced string : Tutorials(p(oin)t(...))
- Related Articles
- Removing Brackets from an Algebraic String Containing + and – Operators using C++
- Finding the balance of brackets in JavaScript
- Parse and balance angle brackets problem in JavaScript
- Find one extra character in a string using C++.
- Binary tree to string with brackets in C++
- Validating brackets in a string in JavaScript
- Program to find string after removing consecutive duplicate characters in Python
- Find an equal point in a string of brackets using C++.
- Maximum points covered after removing an Interval in C++
- Program to find shortest string after removing different adjacent bits in Python
- In SAP Crystal Reports, removing extra white space between details sections
- C++ code to find xth element after removing numbers
- Remove extra spaces in string JavaScript?
- Check if a + b = c is valid after removing all zeroes from a, b and c in C++
- C program to remove extra spaces using string concepts.

Advertisements