

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find an equal point in a string of brackets using C++.
Here we will see how to get the equal points in a string of brackets. The equal point is the index I, such that the number of opening brackets before it is equal to the number of the closing bracket after it. Suppose a bracket string is like “(()))(()()())))”, if we see closer, we can get
So the number of opening brackets from 0 to 9 is 5, and the number of the closing brackets from 9 to 14 is also 5, so this is the equal point.
To solve this problem, we have to follow these few steps −
- Store the number of opening brackets appears in the string up to every index i
- Store the number of closing brackets appears in the string up to each and every index I but it should be done from the last index.
- Check whether an index has the same value of opening and closing brackets.
Example
#include<iostream> #include<cmath> using namespace std; int findEqualPoint(string str) { int total_length = str.length(); int open[total_length+1] = {0}, close[total_length+1] = {0}; int index = -1; open[0] = 0; close[total_length] = 0; if (str[0]=='(') //check whether first bracket is opening or not, if so mark open[1] = 1 open[1] = 1; if (str[total_length-1] == ')') //check whether first bracket is closing or not, if so mark close[end] = 1 close[total_length-1] = 1; for (int i = 1; i < total_length; i++) { if ( str[i] == '(' ) open[i+1] = open[i] + 1; else open[i+1] = open[i]; } for (int i = total_length-2; i >= 0; i--) { if ( str[i] == ')' ) close[i] = close[i+1] + 1; else close[i] = close[i+1]; } if (open[total_length] == 0) return total_length; if (close[0] == 0) return 0; for (int i=0; i<=total_length; i++) if (open[i] == close[i]) index = i; return index; } int main() { string str = "(()))(()()())))"; cout << "Index of equal point: " << findEqualPoint(str); }
Output
Index of equal point: 9
- Related Questions & Answers
- Validating brackets in a string in JavaScript
- Find a Fixed Point (Value equal to index) in a given array in C++
- Find Equal (or Middle) Point in a sorted array with duplicates in C++
- Find a Fixed Point (Value equal to index) in a given array in C++ Program
- Balance a string after removing extra brackets in C++
- Find digits not between the brackets using JavaScript Regular Expressions?
- Find digits between brackets in JavaScript RegExp?
- Binary tree to string with brackets in C++
- Find a Fixed Point in an array with duplicates allowed in C++
- Find number of spaces in a string using JavaScript
- Find intersection point of lines inside a section in C++
- Finding score of brackets in JavaScript
- Rotation of a point about another point in C++
- Find the Number of Substrings of a String using C++
- Find a partition point in array in C++
Advertisements