Given a string with brackets. If the start index of the open bracket is given, find the index of the closing bracket.
Input : string = [ABC] index = 0 Output : 8 The opening bracket at index 0 corresponds to closing bracket at index 8.
The idea is to use Stack data structure. We traverse given expression from given index and keep pushing starting brackets. Whenever we encounter a closing bracket, we pop a starting bracket. If stack becomes empty at any moment, we return that index.
[ABC], 0: 8 [ABC], 4: 7 [ABC], 9: 12 [ABC], 1: -1
Time Complexity: O(n)
Auxiliary Space: O(n)
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.
- Number of closing brackets needed to complete a regular bracket sequence
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Expression contains redundant bracket or not
- Check if expression contains redundant bracket or not | Set 2
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Minimum number of bracket reversals needed to make an expression balanced
- Print Bracket Number
- Minimum Swaps for Bracket Balancing
- Number of balanced bracket subsequence of length 2 and 4
- Range Queries for Longest Correct Bracket Subsequence Set | 2
- Range Queries for Longest Correct Bracket Subsequence
- Print all ways to break a string in bracket form
- Construct Binary Tree from String with bracket representation
- Convert an unbalanced bracket sequence to a balanced sequence
- Find if an expression has duplicate parenthesis or not
- Find all the patterns of "1(0+)1" in a given string | SET 2(Regular Expression Approach)
- Find last index of a character in a string
- Find the Mid-Alphabet for each index of the given Pair of Strings
- How to find index of any Currency Symbols in a given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.