Open In App

Find an equal point in a string of brackets

Last Updated : 25 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:   4
Explanation: 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: -1
Explanation: 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.

Untitled

[Naive Approach] Using Nested Loops â€“ O(n^2) Time and O(1) Space

  • We 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));  

Output
4

[Expected Approach] Using One variable – O(n) Time and O(1) Space

  • We 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


Next Article
Article Tags :
Practice Tags :

Similar Reads