Find an equal point in a string of brackets Last Updated : 25 Mar, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given a string of brackets, the task is to find an index k which decides the number of opening brackets is equal to the number of closing brackets. The string must be consists of only opening and closing brackets i.e. '(' and ')'.An equal point is an index such that the number of opening brackets before it is equal to the number of closing brackets from and after.If multiple such points exist then return the first valid index and if no such index exists then return -1.Examples: Input: str = "(())))("Output: 4Explanation: After index 4, string splits into (()) and ))(. The number of opening brackets in the first part is equal to the number of closing brackets in the second part.Input: str = "))"Output: -1Explanation: NO such index exists such that the count of opening brackets before that is equal to the count of closing brackets from and after that.Table of Content[Naive Approach] Using Nested Loops – O(n^2) Time and O(1) Space[Expected Approach] Using One variable – O(n) Time and O(1) Space[Naive Approach] Using Nested Loops – O(n^2) Time and O(1) SpaceWe iterates through each possible index , counts the number of opening brackets ('(') before this index, and counts the number of closing brackets (')') after this index .If the counts are equal, we returns the index .If no such index is found, it returns -1. C++ #include <bits/stdc++.h> using namespace std; int findEqlPoint(string s) { int n = s.size(); for (int i = 0; i < n; i++) { int openCnt = 0, closeCnt = 0; //count opening brackets before i index and closing brackets after i index for (int j = 0; j < i; j++){ if (s[j] == '(') openCnt++; } for (int j = i; j < n; j++){ if (s[j] == ')') closeCnt++; } if (openCnt == closeCnt) return i; } return -1; } int main() { string s = "(())))("; cout << findEqlPoint(s) << endl; return 0; } Java public class Main { public static int findEqlPoint(String s) { int n = s.length(); for (int i = 0; i < n; i++) { int openCnt = 0, closeCnt = 0; // Count opening brackets before i index and closing brackets after i index for (int j = 0; j < i; j++) { if (s.charAt(j) == '(') openCnt++; } for (int j = i; j < n; j++) { if (s.charAt(j) == ')') closeCnt++; } if (openCnt == closeCnt) return i; } return -1; } public static void main(String[] args) { String s ="(())))("; System.out.println(findEqlPoint(s)); } } Python def findEqlPoint(s): n = len(s) for i in range(0, n): openCnt, closeCnt = 0, 0 # Count opening brackets before i index and closing brackets after i index for j in range(i): if s[j] == '(': openCnt += 1 for j in range(i, n): if s[j] == ')': closeCnt += 1 if openCnt == closeCnt: return i return -1 s = "(())))(" print(findEqlPoint(s)) C# using System; class Program { public static int FindEqlPoint(string s) { int n = s.Length; for (int i = 0; i < n; i++) { int openCnt = 0, closeCnt = 0; // Count opening brackets before i index and closing brackets after i index for (int j = 0; j < i; j++) { if (s[j] == '(') openCnt++; } for (int j = i; j < n; j++) { if (s[j] == ')') closeCnt++; } if (openCnt == closeCnt) return i; } return -1; } static void Main() { string s = "(())))("; Console.WriteLine(FindEqlPoint(s)); } } JavaScript function findEqlPoint(s) { let n = s.length; for (let i = 0; i < n; i++) { let openCnt = 0, closeCnt = 0; // Count opening brackets before i index and closing brackets after i index for (let j = 0; j < i; j++) { if (s[j] === '(') openCnt++; } for (let j = i; j < n; j++) { if (s[j] === ')') closeCnt++; } if (openCnt === closeCnt) return i; } return -1; } let s ="(())))("; console.log(findEqlPoint(s)); Output4 [Expected Approach] Using One variable – O(n) Time and O(1) SpaceWe first count the total number of closing brackets and then iterate over the string.We update the counts of opening and closing brackets at every index as we traverse the string, and when they are equal, we return the index .if no such index exists we return -1. C++ #include <bits/stdc++.h> using namespace std; int findEqlPoint(string s) { int n = s.size(), openCnt = 0, closeCnt = 0; // pre calculate the no of closing brackets for (int i = 0; i < n; ++i){ if (s[i] == ')') closeCnt++; } for (int i = 0; i < n; i++) { if (openCnt == closeCnt) return i; // no of opening brackets before index i+1 if (s[i] == '(') openCnt++; //no of closing brackets at and after index i+1 if (s[i] == ')') closeCnt--; } return -1; } int main() { string s = "))"; cout << findEqlPoint(s) << endl; return 0; } Java public class Main { public static int findEqlPoint(String s) { int n = s.length(), openCnt = 0, closeCnt = 0; // Pre calculate the number of closing brackets for (int i = 0; i < n; ++i) { if (s.charAt(i) == ')') closeCnt++; } for (int i = 0; i < n; i++) { if (openCnt == closeCnt) return i; // Number of opening brackets before index i+1 if (s.charAt(i) == '(') openCnt++; // Number of closing brackets at and after index i+1 if (s.charAt(i) == ')') closeCnt--; } return -1; } public static void main(String[] args) { String s = "))"; System.out.println(findEqlPoint(s)); } } Python def findEqlPoint(s): n = len(s) openCnt, closeCnt = 0, 0 # Pre calculate the number of closing brackets for i in range(n): if s[i] == ')': closeCnt += 1 for i in range(n): if openCnt == closeCnt: return i # Number of opening brackets before index i+1 if s[i] == '(': openCnt += 1 # Number of closing brackets at and after index i+1 if s[i] == ')': closeCnt -= 1 return -1 s = "))" print(findEqlPoint(s)) C# using System; class Program { public static int FindEqlPoint(string s) { int n = s.Length, openCnt = 0, closeCnt = 0; // Pre calculate the number of closing brackets for (int i = 0; i < n; ++i) { if (s[i] == ')') closeCnt++; } for (int i = 0; i < n; i++) { if (openCnt == closeCnt) return i; // Number of opening brackets before index i+1 if (s[i] == '(') openCnt++; // Number of closing brackets at and after index i+1 if (s[i] == ')') closeCnt--; } return -1; } static void Main() { string s = "))"; Console.WriteLine(FindEqlPoint(s)); } } JavaScript function findEqlPoint(s) { let n = s.length; let openCnt = 0, closeCnt = 0; // Pre calculate the number of closing brackets for (let i = 0; i < n; ++i) { if (s[i] === ')') closeCnt++; } for (let i = 0; i < n; i++) { if (openCnt === closeCnt) return i; // Number of opening brackets before index i+1 if (s[i] == '(') openCnt++; // Number of closing brackets at and after index i+1 if (s[i] == ')') closeCnt--; } return -1; } let s = "))"; console.log(findEqlPoint(s)); Output-1 Comment More infoAdvertise with us Next Article Find an equal point in a string of brackets S Sahil Chhabra (akku) and Sankararaman K Improve Article Tags : Strings DSA Arrays Amazon Practice Tags : AmazonArraysStrings Similar Reads Print all ways to break a string in bracket form Given a string, find all ways to break the given string in bracket form. Enclose each substring within a parenthesis. Examples: Input : abc Output: (a)(b)(c) (a)(bc) (ab)(c) (abc) Input : abcd Output : (a)(b)(c)(d) (a)(b)(cd) (a)(bc)(d) (a)(bcd) (ab)(c)(d) (ab)(cd) (abc)(d) (abcd) We strongly recomm 4 min read Count number of equal pairs in a string Given a string s, find the number of pairs of characters that are same. Pairs (s[i], s[j]), (s[j], s[i]), (s[i], s[i]), (s[j], s[j]) should be considered different. Examples : Input: airOutput: 3Explanation :3 pairs that are equal are (a, a), (i, i) and (r, r)Input : geeksforgeeksOutput : 31Recommen 8 min read Balance a string after removing extra brackets Given a string of characters with opening and closing brackets. The task is to remove extra brackets from string and balance it. Examples: Input: str = "gau)ra)v(ku(mar(rajput))" Output: gaurav(ku(mar(rajput))) Input: str = "1+5)+5+)6+(5+9)*9" Output: 1+5+5+6+(5+9)*9 Approach: Start traversing from 6 min read Find repeated character present first in a string Given a string, find the repeated character present first in the string.(Not the first repeated character, found here.) Examples: Input : geeksforgeeks Output : g (mind that it will be g, not e.) Asked in: Goldman Sachs internship Simple Solution using O(N^2) complexity: The solution is to loop thro 15 min read Print the balanced bracket expression using given brackets Given four integers a, b, c and d which signifies the number of four types of brackets. "((""()"")(""))" The task is to print any balanced bracket expression using all the given brackets. If we cannot form a balanced bracket expression then print -1. In case of multiple answers, print any one. Examp 6 min read Remove brackets from an algebraic string containing + and - operators Given an algebraic expression as a string s containing operands (letters), + and - operators, and parentheses, simplify the expression by removing all parentheses and correctly applying the operators. Return the simplified expression without parentheses.Examples: Input:"(a - (b + c) + d)"Output: "a- 6 min read Check if a string is substring of another Given two strings txt and pat, the task is to find if pat is a substring of txt. If yes, return the index of the first occurrence, else return -1.Examples : Input: txt = "geeksforgeeks", pat = "eks"Output: 2Explanation: String "eks" is present at index 2 and 9, so 2 is the smallest index.Input: txt 8 min read Make string S equal to T after replacing some prefix characters in S Given two strings S and T of equal length consisting of lowercase characters. In one operation, it is allowed to erase the 0th character of the string S and move it to any place in the string S, the task is to find the minimum number of operations required to convert S equal to T. Examples: Input: S 7 min read Find index of closing bracket for a given opening bracket in an expression Given a string with brackets. If the start index of the open bracket is given, find the index of the closing bracket. Examples: Input : string = [ABC[23]][89] index = 0 Output : 8 The opening bracket at index 0 corresponds to closing bracket at index 8.Recommended PracticeClosing bracket indexTry It 7 min read Minimum swaps to balance the given brackets at any index Given a balanced string of even length consisting of equal number of opening brackets â[â and closing brackets â]â , Calculate the minimum number of swaps to make string balanced. An unbalanced string can be made balanced by swapping any two brackets. A string is called balanced if it can be represe 5 min read Like