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.
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.
Input : str = "(())))(" Output: 4 After index 4, string splits into (()) and ))(. Number of opening brackets in the first part is equal to number of closing brackets in the second part. Input : str = "))" Output: 2 As after 2nd position i.e. )) and "empty" string will be split into these two parts: So, in this number of opening brackets i.e. 0 in the first part is equal to number of closing brackets in the second part i.e. also 0.
Asked in : Amazon
- Store the number of opening brackets appears in the string up to every index, it must start from starting index.
- Similarly, Store the number of closing brackets appears in the string upto each and every index but it should be done from last index.
- Check if any index has the same value of opening and closing brackets.
Time Complexity : O(n)
This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find a Fixed Point (Value equal to index) in a given array
- Find Equal (or Middle) Point in a sorted array with duplicates
- Find a Fixed Point (Value equal to index) in a given array | Duplicates Allowed
- String Range Queries to find the number of subsets equal to a given String
- Binary tree to string with brackets
- Balance a string after removing extra brackets
- Check if a given string is a valid number (Integer or Floating Point) in Java
- Find a partition point in array
- Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach)
- Find the transition point in a binary array
- Find a partition point in array to maximize its xor sum
- Find a point that lies inside exactly K given squares
- Find a Fixed Point in an array with duplicates allowed
- Find the number of points that have atleast 1 point above, below, left or right of it
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Check if two expressions with brackets are same
- Divide a string in N equal parts
- Printing brackets in Matrix Chain Multiplication Problem
- Count equal pairs from given string arrays
- Longest sub-string having frequency of each character less than equal to k